Hi, I'm just wondering how I could remove everything after a certain substring in PHP
ex:
Posted On April 6th By Some Dude
I'd like to have it so that it removes all the text including, and after, the sub string "By"
Thanks
Hi, I'm just wondering how I could remove everything after a certain substring in PHP
ex:
Posted On April 6th By Some Dude
I'd like to have it so that it removes all the text including, and after, the sub string "By"
Thanks
$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.
You could do:
$posted = preg_replace('/ By.*/', '', $posted);
echo $posted;
preg_replace
offers one way:
$newText = preg_replace('/\bBy.*$/', '', $text);
$var = "Posted On April 6th By Some Dude";
$new_var = substr($var, 0, strpos($var, " By");
Austin's answer works for your example case.
More generally, you would do well to look into the regular expression functions when the substring you're splitting on may differ between strings:
$variable = preg_replace('/By.*/', '', $variable);
By using regular expression: $string = preg_replace('/\s+By.*$/', '', $string)