How to Remove Part of a String in PHP

Remove portion of a string after a certain character

$variable = substr($variable, 0, strpos($variable, "By"));

In plain english: Give me the part of the string starting at the beginning and ending at the position where you first encounter the deliminator.

PHP - Remove part of string?

You may use something like this:

$str = "AAA-BBB-CCC";
$str2 = explode("-", $str);
array_shift($str2);
$str = implode("-", $str2);

How to remove a substring from a string using PHP?

If you want to match any width/height values:

  $path = "http://thedude.com/05/simons-cat-and-frog-100x100.jpg";

// http://thedude.com/05/simons-cat-and-frog.jpg
echo preg_replace( "/-\d+x\d+/", "", $path );

Demo: http://codepad.org/cnKum1kd

The pattern used is pretty basic:

/     Denotes the start of the pattern
- Literal - character
\d+ A digit, 1 or more times
x Literal x character
\d+ A digit, 1 or more times
/ Denotes the end of the pattern

Remove specific string from string input in PHP

As i first commented, instead of preg_replace() use str_replace() like below:-

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

so code will be:-

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

Output:- https://eval.in/609265

Remove a string from the beginning of a string

Plain form, without regex:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';

if (substr($str, 0, strlen($prefix)) == $prefix) {
$str = substr($str, strlen($prefix));
}

Takes: 0.0369 ms (0.000,036,954 seconds)

And with:

$prefix = 'bla_';
$str = 'bla_string_bla_bla_bla';
$str = preg_replace('/^' . preg_quote($prefix, '/') . '/', '', $str);

Takes: 0.1749 ms (0.000,174,999 seconds) the 1st run (compiling), and 0.0510 ms (0.000,051,021 seconds) after.

Profiled on my server, obviously.

Remove a part of a string, but only when it is at the end of the string

You'll note the use of the $ character, which denotes the end of a string:

$new_str = preg_replace('/string$/', '', $str);

If the string is a user supplied variable, it is a good idea to run it through preg_quote first:

$remove = $_GET['remove']; // or whatever the case may be
$new_str = preg_replace('/'. preg_quote($remove, '/') . '$/', '', $str);

Remove Parts of String in php

You can use the function str_replace() to replace "He" for "". The code would look like this:

$string = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$substring = "GHIJKLM";
$final_string = str_replace($substring, "", $string);
echo $final_string;

The output would be:

ABCDEFNOPQRSTUVWXYZ

The str_replace() function will replace a substring, in your case "GHIJKLM", for an other string, in this case we will use "" because we want to remove it from the $string.

PHP function to delete all between certain character(s) in string

<?php

$string = 'Some valid and <script>some invalid</script> text!';
$out = delete_all_between('<script>', '</script>', $string);
print($out);

function delete_all_between($beginning, $end, $string) {
$beginningPos = strpos($string, $beginning);
$endPos = strpos($string, $end);
if ($beginningPos === false || $endPos === false) {
return $string;
}

$textToDelete = substr($string, $beginningPos, ($endPos + strlen($end)) - $beginningPos);

return delete_all_between($beginning, $end, str_replace($textToDelete, '', $string)); // recursion to ensure all occurrences are replaced
}


Related Topics



Leave a reply



Submit