My variables look like this:
AAAAAAA, BB CCCCCCCC
AAAA,BBBBBB CCCCCC
I would like to remove everything before the ",
",
so the results should look like:
BB CCCCCCCC
BBBBBB CCCCCC
I have worked out this to remove everything AFTER the ",
":
list($xxx) = explode(',', $yyyyy);
unfortunately I dont know how to get it to work to remove everything BEFORE the ",
".
Press Ctrl + H to open the Find and Replace dialog. In the Find what box, enter one of the following combinations: To eliminate text before a given character, type the character preceded by an asterisk (*char). To remove text after a certain character, type the character followed by an asterisk (char*).
lstrip() #strips everything before and including the character or set of characters you say. If left blank, deletes whitespace.. rstrip() #strips everything out from the end up to and including the character or set of characters you give. If left blank, deletes whitespace at the end.
trim() . trim() removes spaces before the first character (which isn't a whitespace, such as letters, numbers etc.) of a string (leading spaces) and also removes spaces after the last character (trailing spaces).
Since this is a simple string manipulation, you can use the following to remove all characters before the first comma:
$string = preg_replace('/^[^,]*,\s*/', '', $input);
preg_replace()
allows you to replace parts of a string based on a regular expression. Let's take a look at the regular expression.
/ is the start delimiter
^ is the "start of string" anchor
[^,] every character that isn't a comma (^ negates the class here)
* repeated zero or more times
, regular comma
\s any whitespace character
* repeated zero or more times
/ end delimiter
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With