php_forever Posted November 16, 2014 Share Posted November 16, 2014 All right, so I've been sober for YEARS and now all of a sudden I'm tempted to have just ONE..... OOPS, wrong forum :-) Okay, here's my goal: to replace anything beginning with the first instance of "dog" and the last instance of "cat" with the word "animal." Note: everything's case-insensitive, AND, sometimes there are characters between dog and cat (example: dogxxxxcat) and sometimes there are no characters between dog and cat (example: lfjljfdjldogcatlsfjsjsljslj) Examples: $string = 'My dogcat is cool'; (becomes My animal is cool) $string = 'Wonderfuldogcatandappleappleapplecatcatcat'; (becomes Wonderfulanimal) $string = 'dog cat'; (becomes animal) $string = 'dogcatdogcatdogcatdogcatdogcatdog'; (becomes animaldog) I know the answer is probably easy, I'm not trying to waste anybody's time -- but I've been googling this for nearly 3 days, I think I have probably "seen" the answer, but have a mental block, I just can't figure it out. Thank you!! Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/ Share on other sites More sharing options...
requinix Posted November 16, 2014 Share Posted November 16, 2014 What's the most obvious thing to try? Have you tried it? Or what have you tried? Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496705 Share on other sites More sharing options...
tryingtolearn Posted November 16, 2014 Share Posted November 16, 2014 Your goal actually spells it all out for you! Okay, here's my goal: to replace anything beginning with the first instance of "dog" and the last instance of "cat" with the word "animal." beginning with the first instance of "dog" = stripos [Find the position of the first occurrence of a case-insensitive substring in a string] the last instance of "cat" = strripos [Find the position of the last occurrence of a case-insensitive substring in a string] replace anything ... with the word "animal." = substr_replace [Replace text within a portion of a string] I would also look into strlen when you get to strripos. Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496717 Share on other sites More sharing options...
NotionCommotion Posted November 16, 2014 Share Posted November 16, 2014 Your goal actually spells it all out for you! beginning with the first instance of "dog" = stripos [Find the position of the first occurrence of a case-insensitive substring in a string] the last instance of "cat" = strripos [Find the position of the last occurrence of a case-insensitive substring in a string] replace anything ... with the word "animal." = substr_replace [Replace text within a portion of a string] I would also look into strlen when you get to strripos. I could be wrong, but it sounds like he is trying to learn regex, and wanted to use regex for this example. I messed around with it for a bit, but I am really bad at regex. Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496728 Share on other sites More sharing options...
tryingtolearn Posted November 16, 2014 Share Posted November 16, 2014 Guess you are right, missed that in the title... Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496744 Share on other sites More sharing options...
NotionCommotion Posted November 16, 2014 Share Posted November 16, 2014 Guess you are right, missed that in the title... But it is the thought that counts Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496745 Share on other sites More sharing options...
tryingtolearn Posted November 16, 2014 Share Posted November 16, 2014 Didn't even think of using regular expressions for the example posted Seemed a straight forward use for the functions I listed.. Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496746 Share on other sites More sharing options...
NotionCommotion Posted November 16, 2014 Share Posted November 16, 2014 I don't know. Maybe he is really interested in cats and dogs, and your solution is perfect. Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496747 Share on other sites More sharing options...
requinix Posted November 16, 2014 Share Posted November 16, 2014 String functions certainly could do it, though it's a little harder since you need to do error checking (eg, what if there is no "dog" or "cat", or what if "dog" comes after "cat"). The regex version would be $output = preg_replace('/dog.*cat/', 'animal', $input); Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496767 Share on other sites More sharing options...
tryingtolearn Posted November 16, 2014 Share Posted November 16, 2014 Good point, it is a bit more with the string functions <?php //$string = 'My dogcat is cool'; //(becomes My animal is cool) //$string = 'Wonderfuldogcatandappleappleapplecatcatcat'; //(becomes Wonderfulanimal) //$string = 'dog cat'; //(becomes animal) //$string = 'dogcatdogcatdogcatdogcatdogcatdog'; //(becomes animaldog) //$string = 'catdogcatdogcatdogcatdogcatdogcatdog'; //(becomes animaldog) //$string = 'cat cat cat'; $string = 'cat dog dog cat dog'; $start = "dog"; $stop = "cat"; $replace = "animal"; //get Start Position $startPos = stripos($string, $start); //get Stop Position $stopPos = strripos($string, $stop); if($stopPos === false || $startPos === false){ echo "Sorry, We couldnt find a string to replace!<br /> $string"; }else{ echo substr_replace($string, $replace, $startPos, ($stopPos+strlen($stop))) . "<br />\n"; } ?> If it can be done w/ string functions I tend to lean that way Guess its the intimidation factor Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496769 Share on other sites More sharing options...
NotionCommotion Posted November 17, 2014 Share Posted November 17, 2014 The regex version would be I really gotta get this regex thing licked! Quote Link to comment https://forums.phpfreaks.com/topic/292494-naive-dog-and-cat-regex-question/#findComment-1496771 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.