Forum Moderators: coopster

Message Too Old, No Replies

preg replace question

preg_replace

         

beneb

4:33 pm on Sep 22, 2009 (gmt 0)

10+ Year Member



I've got a simple preg_replace script to transform the characters from a string into a matching image of that character. Problem is the preg_replace changes any character including the ones inside the <img> tag.

Is there anyway to make it skip the tags and all use the characters from the actual string?

I tried splitting the string first but that just returns the word 'Array' on the screen.

Here is the script:

--

$str = "a cat a dog and a fish";

$arr1 = str_split($str);

$patterns[0] = '/a/';
$patterns[1] = '/b/';
$patterns[2] = '/c/';
$patterns[3] = '/d/';
$patterns[4] = '/e/';
$patterns[5] = '/f/';
$patterns[6] = '/g/';
$replacements[0] = "<img src=\"images/a.jpg\">";
$replacements[1] = "<img src=\"images/b.jpg\">";
$replacements[2] = "<img src=\"images/c.jpg\">";
$replacements[3] = "<img src=\"images/d.jpg\">";
$replacements[4] = "<img src=\"images/e.jpg\">";
$replacements[5] = "<img src=\"images/f.jpg\">";
$replacements[6] = "<img src=\"images/g.jpg\">";
$final = preg_replace($patterns, $replacements, $str);

print $final;

--

Many thanks

whoisgregg

10:44 pm on Sep 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It looks like you've attempted to accomplish this by splitting the string into an array?

$arr1 = str_split($str);

We'll stick with this method, although there are probably a dozen ways to solve this particular problem. :)

$arr1 = str_split($str);
$valid_img = '/[a-zA-Z0-9]/'; // this defines what characters for which you have images available
foreach($arr1 as $key=>$value){
if(preg_match($valid_img, $value)){
$arr1[$key] = '<img src="images/'.$value.'.jpg">';
}
}
echo implode('',$arr1);

beneb

2:00 am on Sep 23, 2009 (gmt 0)

10+ Year Member



Ahh fantastic, that is great. Thankyou so much.

One other question, I would also like to replace the space between words with an image of a blank space. Is that possible through the same method?
I tried putting different combinations of space into the original code but to no avail...

andrewsmd

1:56 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Putting a bunch of blank spaces in code will not get displayed in html because it only displays one blank space by default. Since you didn't post any code I'm guessing here but where ever you want the blank spaces put a &nbsp; and that will give you a space in html. If you want a bunch just keep putting them in. Post some code if you need more help.

andrewsmd

1:57 pm on Sep 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Note: &nbsp; is text, not php so you would echo it as echo("&nbsp;");

rocknbil

6:36 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The array bit is a little convoluted.

It looks to me like you're swapping "a" with an image of an A, etc. Is this correct?

<?php
header("content-type:text/html");
$str = "a cat a dog and a fish and a 1 2 three 4";
$pre = '<img src="';
$post = '.jpg">';
$imgstring = '<img src="space.jpg">';

echo "string: $str<br>\n";
$str = preg_replace('/([A-Z\d])/i', "$pre$1$post", $str);
$str = preg_replace('/(>)\s+(<)/i', "$1$imgstring$2", $str);
// This is your actual output.
//echo $str;
//This is to display code in this test
echo htmlentities($str);
?>

Note we also have number images too. You may run into trouble if the string begins or ends with a space, but the above appears to work as expected.

If this is for what I think it's for - CAPTCHA or email obfuscation - there are better ways. :-)

[edited by: eelixduppy at 10:11 pm (utc) on Sep. 24, 2009]
[edit reason] disabled smileys [/edit]

whoisgregg

7:58 pm on Sep 24, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using the array method I posted, to also replace spaces, you just add it to the regular expression:

$valid_img = '/[[:space:]a-zA-Z0-9]/'; // this defines what characters for which you have images available