PHP remove all characters before specific string
You can use strstr to do this.
echo strstr($str, 'www/audio');
Removing all characters before certain string
There is a php built in for doing this: strstr
Combine with substr to strip out your token:
$out = substr(strstr($text, 'ABC'), strlen('ABC'))
Remove characters before and including a specific symbol
Try using preg_replace
:
preg_replace("/.+?( –)/", '', $linktitle)
If you want to remove the whitespace after the -
too:
preg_replace("/.+?( –)\s*/", '', $linktitle)
This uses regular expression to match a pattern defined by any character except newline 1 or more times (.+
), until it meets (?
) a space followed by a dash (( –)
), then a whitespace (\s
) 0 or more times (*
). Preg_replace then replaces the matched pattern with an empty string.
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.
Remove everything up to and including character in PHP string
This can be achieved using string manipulation functions in PHP. First we find the position of the -
character in the string using strpos()
. Use substr()
to get everything until that character (including that one). Then use trim()
to remove whitespace from the beginning and/or end of the string:
echo trim(substr($str, strpos($str, '-') + 1)); // => £20.00
Alternatively, you could split the string into two pieces, with -
as the delimiter, and take the second part:
echo trim(explode('-', $str)[1]);
This could be done in many different ways. In the end, it all boils down to your preferences and requirements.
Cut a string before a character in better way in php
For this example, alternatively, you could use strtok()
also:
$string = 'This is a simple sting | badword is here';
$result = strtok($string, '|');
echo $result; // This is a simple sting
How to remove everything before the first specific character in a string?
I wouldn't recommend using explode, as it causes more issues if there is more than one comma.
// removes everything before the first ,
$new_str = substr($str, ($pos = strpos($str, ',')) !== false ? $pos + 1 : 0);
Edit:
if(($pos = strpos($str, ',')) !== false)
{
$new_str = substr($str, $pos + 1);
}
else
{
$new_str = get_last_word($str);
}
PHP remove all characters before, except last number
preg_match('/(\d+)\.ts$/', $test, $matches);
echo $matches[1];
PHP remove all characters before certain string
Using regex:
$str = "drog stabilizatorja Meyle RE 16-16 060 0004/HD";
preg_match("/[^\\d*](\\d.*)/", $str, $matches);
echo $matches[1];
Output:
16-16 060 0004/HD
How to remove substring up to a certain character?
Here's a method using the substr() and strpos() functions that will work:
$str = substr($str, strpos($str, '/') + 1 );
Related Topics
What's the Most Efficient Test of Whether a PHP String Ends with Another String
How to Pass Custom Type Array to Postgres Function
Whats the Easiest Way to Determine If a User Is Online? (Php/Mysql)
PHP Readdir Problem with Japanese Language File Name
How to Remove the Leading Character from a String
Highlight Keywords in a Paragraph
Check to See If a String Is Serialized
Laravel Named Route for Resource Controller
Why Is PHP Not Replacing the Variable in String
Laravel Advanced Wheres How to Pass Variable into Function
Dynamically Generating a Qr Code with PHP
Pull All Images from a Specified Directory and Then Display Them
Difference Between Var_Dump,Var_Export & Print_R
How to Compare 2 HTML Pages, and Output Only the Different Bits in Ruby or PHP
PHP SQL Join Together Multiple Tables from Different Databases
Understanding Imagemagick's Convert and Translating to Ruby Rmagick