$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.
If you're using PHP 5.3+ take a look at the $before_needle flag of strstr()
$s = 'Posted On April 6th By Some Dude';
echo strstr($s, 'By', true);
How about using explode
:
$input = 'Posted On April 6th By Some Dude';
$result = explode(' By',$input);
return $result[0];
Advantages:
$result[1]
would return Some Dude
in this example)You could do:
$posted = preg_replace('/ By.*/', '', $posted);
echo $posted;
This is a regular expression replacer function that finds the literal string ' By
' and any number of characters after it (.*
) and replaces them with an empty string (''
), storing the result in the same variable ($posted
) that was searched.
If [space]By
is not found in the input string, the string remains unchanged.
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