Find, evaluate and replace

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
conquesimo
Forum Newbie
Posts: 10
Joined: Mon May 26, 2008 3:30 pm

Find, evaluate and replace

Post by conquesimo »

I'm new to PHP and have just learned how to use regular expressions earlier today. I'm trying to fetch a string, match a desired pattern within the string, analyze the match, and according to the results of the analysis, replace the match with some newly-concocted words. I know this involves using regular expressions and probably has some shortcut function like preg_replace or something, but I haven't been able to figure it out yet.

Here's an example of the string before I process it:

Code: Select all

<body>
Here's a sweet picture!
<< monkeyfish >>
 
Here's another sweet picture!
<< meowplow >>
</body>
Here's an example of the string after it's processed:

Code: Select all

<body>
Here's a sweet picture!
<img src="monkeyfish.jpg">
 
Here's another sweet picture!
<img src="meowplow.jpg">
</body>
Basically, I want to find the words between "<<" and ">>", isolate the words, add text before and after the words, then replace "<< words >>" with my new creation. What's the best way to accomplish this?
hansford
Forum Commoner
Posts: 91
Joined: Mon May 26, 2008 12:38 am

Re: Find, evaluate and replace

Post by hansford »

There is a much better way to do this, but I'm not that good with regex, so I had to write some goofy code. In your html file that you want to modify I changed it so it looks like this:

Here's a sweet picture!
{monkeyfish}

Here's another sweet picture!
{meowplow}
-------------------------------------
$path = $_SERVER['DOCUMENT_ROOT'] . "/yourfiles/yourfile.html";
if(file_exists($path)){

try{

$f = file_get_contents($path);
$match = "/\{([A-Za-z])+\}/";

preg_match_all($match,$f,$preg_array);
$matcharray = array();
$matches = $preg_array[0];

for($i=0;$i<sizeof($matches); $i++){

$src = "";
$mat = $matches[$i];

for($j=0;$j<strlen($mat);$j++){

if(($mat[$j] == '{') || ($mat[$j] == '}')){
continue;
}
$src .= $mat[$j];
}
$matcharray[$i] = $src;
$search = "{" . $matcharray[$i] . "}";
$replace = "<img src='" . $matcharray[$i]. ".jpg'>";
$f = str_replace($search, $replace, $f);
}
}catch(EXCEPTION $e){

echo "File does not exist";
}
echo $f;
}
conquesimo
Forum Newbie
Posts: 10
Joined: Mon May 26, 2008 3:30 pm

Re: Find, evaluate and replace

Post by conquesimo »

Thanks for the code Hansford, but I there's got to be a better way. I want to find, evaluate and replace without having to cycle through the text more than once. The way I have it now, I'm searching for the first occurrence of a regular expression, replacing it, then searching through the entire text once again to find the next match.

I know there has got to be a better way. Ideally, I'm looking for a function that finds the first occurrence of a string with the larger text, evaluates it, immediately replaces it, then continues on its way looking for the next match.
Scrumpy.Gums
Forum Commoner
Posts: 71
Joined: Thu Aug 30, 2007 2:57 pm
Location: Bristol, UK

Re: Find, evaluate and replace

Post by Scrumpy.Gums »

Code: Select all

$input = preg_replace("#<< (.*) >>#", '<img src="\\1">', $input);
Where $input is your original html :wink: for reference: php.net/preg_replace
conquesimo
Forum Newbie
Posts: 10
Joined: Mon May 26, 2008 3:30 pm

Re: Find, evaluate and replace

Post by conquesimo »

That solution would work well for my posted example, however, what I'm trying to do is not the simple. Not every replacement is for a JPG, some replacements reference ads and some reference widgets. Anyway, I've just figured it out for anyone who is curious.

Here is the code I used:

Code: Select all

$pattern = "/<< *([-*\.*_*0-9a-zA-Z]*) *>>/e";
echo preg_replace($pattern, "myPHPFunction('\\1')", $story);
It does exactly what I wanted. It finds whatever text is between '<<' and '>>', passes the match on a function I wrote named "myPHPFunction", that function returns a new string and then preg_replace sews the new string into the original string.

Thanks for the pointers :-)
Post Reply