i want to remove every thing in my string before the - $url = "jdhsfiusdif-keep this" i also want to remove - i want only to keep "keep this" want your help
$before = strstr($url, '-', true); PHP: If you don't have PHP5, but rather PHP4, use the following: $before = substr($url, 0, strpos($url, '-')); PHP:
Hy, You can use preg_replace(): <?php $url = "jdhsfiusdif-keep this"; $remove = '/^([^-]+-)/'; $url2 = preg_replace($remove, '', $url); echo $url2; // keep this ?> PHP:
Oh apologies, I read your post incorrectly. You want "keep this", in which case: $url = substr(strstr($url, '-'), 1); PHP: My original example keeps everything before the first '-'. This one keeps everything after the first '-'. As for MarPlo's example, it'll likely work fine but strstr is much more efficient, faster.
They don't do the same thing. substr returns a substring from a starting position to an ending position in a string. strstr returns the part of a string from the position of a given needle. Can't be compared like that really unless you were referring to my substr/strpos combination above, in which case I did use substr.
I'd do it like this. function keepthis($var){ $str = explode("-",$var); return $str[1]; } Code (markup):
Then you're left with the problem which occurs when $var = 'abc-def-ghi'. It'd return 'def' rather than 'def-ghi'. In this case it'd work fine though.