Remove specific string from string input in PHP

13.8k Views Asked by At

I have an string from title, and i want to remove specific character/string from this. Title is show with bloginfo('sitename').

I try to made something like that:

<?php
   $title = preg_replace('/'. preg_quote('SRL', '/') . '$/', '', bloginfo('sitename'));
   print $title;
?>

...but don't work.

The title input is: SOMETHING SRL, and i want to show just "SOMETHING".

Thanks for help!

4

There are 4 best solutions below

0
Death-is-the-real-truth On BEST ANSWER

Instead of preg_replace() use str_replace() like below:-

echo trim(str_replace('SRL','',$title)); // first replace `SRL` and then remove extra spaces

so the code will be:-

<?php
   $title = trim(str_replace('SRL','',$title));
   print $title;
?>

Output:- https://3v4l.org/NTrFJ

0
Sagar Prajapati On

Use str_replace() Function

str_replace("SRL","",$title);
0
Hardik On
 $title = "SOMETHING SRL";
 $title = trim(str_replace("SRL","",$title));

Use this code

0
kalpana udara On

You can use str_replace() with trim().

The str_replace() function replaces some characters with some other characters in a string.

str_replace(find,replace,string,count) 

Note: String Parameter is Required. Specifies the string to be searched and count is optional

Use like this

 $stringReplace = "Hello World";
 $stringReplace = trim(str_replace("world","Programmer","Hello world!"));

Output

Hello Programmer