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
Using PHP to Execute Multiple MySQL Queries
Generating a Single Entity from Existing Database Using Symfony2 and Doctrine
Getting Last Month's Date in PHP
A Better Approach Than Storing MySQL Password in Plain Text in Config File
What Is the Format for E-Mail Headers That Display a Name Rather Than the E-Mail
Compile a Referenced Less File into CSS with PHP Automatically
PHP _Php_Incomplete_Class Object with My $_Session Data
Fatal Error: Call to Undefined Function Imap_Open() in PHP
PHP & Sessions: How to Disable PHP Session Locking
PHP - Plus Sign with Get Query
How to Get All Combinations from Multiple Arrays
Running Composer Returns: "Could Not Open Input File: Composer.Phar"
How to Switch from Post to Get in PHP Curl
Run Cron Job on PHP Script, on Localhost in Windows