etrader Posted January 21, 2011 Share Posted January 21, 2011 There are many ways to delete everything after a character in a string. I am looking for a simple way to delete everything after a given word. Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/ Share on other sites More sharing options...
Maq Posted January 21, 2011 Share Posted January 21, 2011 There are many ways to delete everything after a character in a string. I am looking for a simple way to delete everything after a given word. How about we retain everything before and including your desired word. You can do something like this, although, a solution with string functions would be a bit better. ini_set ("display_errors", "1"); error_reporting(E_ALL); $subject = "I want everything before tim and nothing after"; $pattern = '/.* tim/'; preg_match($pattern, $subject, $matches); print_r($matches); ?> Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163217 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 $subject is a string, and this turns it to an array ($matches), am I wight? I need to change the array to string again to display with echo? Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163231 Share on other sites More sharing options...
Maq Posted January 21, 2011 Share Posted January 21, 2011 $subject is a string, and this turns it to an array ($matches), am I wight? I need to change the array to string again to display with echo? No, just: echo $matches[0]; Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163264 Share on other sites More sharing options...
etrader Posted January 21, 2011 Author Share Posted January 21, 2011 This is a brilliant solution, but it has a big problem: it does not work with line breaks. If even writing the php code in two lines as $subject = "I want everything before tim and nothing after"; $pattern = '/.* tim/'; preg_match($pattern, $subject, $matches); print_r($matches); The result will be "before tim" not "I want everything before tim" Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163290 Share on other sites More sharing options...
jcbones Posted January 21, 2011 Share Posted January 21, 2011 Something like: <?php $subject = "I want everything before tim and nothing after"; $word = 'tim'; $subject = substr($subject,0,(strpos($subject,$word) + strlen($word))); echo $subject; ?> Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163300 Share on other sites More sharing options...
Maq Posted January 21, 2011 Share Posted January 21, 2011 There you go, use jcbones's. A solution utilizing string functions is better. But just in case you were wondering, to make the regex solution work with multi lines you would change this line to $pattern = '/(.|\s)* tim/m'; The 'm' modifier indicates, multiline and the pattern accepted one or more any char along with \s (any whitespace character [carriage return, line feed, space and tab]). Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163304 Share on other sites More sharing options...
mikosiko Posted January 21, 2011 Share Posted January 21, 2011 Something like: <?php $subject = "I want everything before tim and nothing after"; $word = 'tim'; $subject = substr($subject,0,(strpos($subject,$word) + strlen($word))); echo $subject; ?> $word should be defined like: $word = ' tim '; to prevent wrong result if the string is something like: $subject = "I want everything in time before tim and nothing after"; right? other option: $subject = "I want everything in time before tim and nothing after"; $word = ' tim '; $arr = explode($word, $string); echo $arr[0]; // if $word need to be ommited too or echo $arr[0] . $word; // to include it Quote Link to comment https://forums.phpfreaks.com/topic/225236-deleting-after-a-given-word-in-a-string/#findComment-1163316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.