Jump to content

Naive Dog and Cat REGEX question?


php_forever

Recommended Posts

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!!

Link to comment
Share on other sites

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.
Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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 ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.