You can do either basic string searching with strpos(), or you can use
regular expressions.
// -------------
// Method 1
// -------------
$str = 'This is MyString and it will be altered';
$find = 'MyString';
if (strpos($str, $find) {
$newStr = substr($str, strpos($str, $find) + strlen($find));
echo $newStr;
// Output: ' and it will be altered'
} else {
echo 'String not found';
}
// -------------
// Method 2 - Regular Expressions
// -------------
$str = 'This is MyString and it will be altered';
$pattern = '/(mystring)/i';
if (preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE)) {
$newStr = substr($str, $matches[0][1] + strlen($matches[0][0]));
echo $newStr;
// output: ' and it will be altered'
} else {
echo 'String not found';
}
I havent tested the above, but its just basic String manipulation, you
shouldnt have any issues modifying it.
Best of luck with it.
Cheers,
Graham Weldon
Kyle Decot wrote:
> How would I find the first occurrence of a word or phrase in a string
> and then strip off everything after that?
>
> I know that I will probably need to use regex but I am unsure how to
> write it.
>
> Any help would be most appreciated.
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment