Forum Moderators: coopster
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
$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);
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]