Re: looping through text with array_key_exists function



Thanks.

I think I must have left my brain on the train today, but what I came
up with earlier was:

function convert_text_to_images($text) {

$split = preg_split('//', strtolower($text), -1, PREG_SPLIT_NO_EMPTY);
print_r($split);


$imgsrc_string="";
$regex = '/[a-z0-9]/i';

for($a = 0; $a <= (count($split)-1); $a++){
if(preg_match($regex, $split[$a])) {$temp =
preg_replace('/([a-z0-9])/', '<img src="$1.jpg" alt="$1">',
$split[$a]); $imgsrc_
string .= $temp;}
elseif ($split[$a]== ' ') {$imgsrc_string .= '<img src="space.jpg"
alt="&nbsp;">';}
elseif ($split[$a]== '!') {$imgsrc_string .= '<img
src="exclamation.jpg" alt="&nbsp;">';}
else {$imgsrc_string .= $string[$a];}

}
return($imgsrc_string);
}

This gives me one large string that I can easily insert into a webpage.

I'm sure if combine elements of what you suggest, I'll soon crack this.

Thanks for your help.
Neil.


Janwillem Borleffs wrote:

drako wrote:
I thought using "preg_match" would work - i.e.

if(preg_match('/([a-z0-9])/', $text)) {preg_replace('/([a-z0-9])/',
'<img src="$1.jpg" />', $text); }


This is superfluous use of preg_* functions, as you can use preg_replace
without the preg_match.

Tricky thing, however, is that img tags contain spaces and you only want to
replace spaces outside the tags.

The following solution offers a workaround for this problem:

$text = 'hello world!';

// replace special cases
$special = array(
' ' => 'space.jpg',
'!' => 'exclamation.jpg',
'_' => 'underscore.jpg'
);

// generate a string from the special characters
$specialchars = preg_quote(implode('', array_keys($special)), '/');

// start replacing; the special characters will generate invalid image
markers (like "_.jpg"),
// which will be replaced during the the second run of preg_replace
$text = preg_replace('/([a-z0-9' . $specialchars . '])/', '<img src="$1.jpg"
/>', $text);

// replace special characters in the img src attributes
$text = preg_replace('/([' . $specialchars . ']).jpg/e', '$special[\'$1\']',
$text);

This way, you will only have to maintain the $special array and leave the
other code as is.

There are other ways of doing this, but the above should give you a push in
the right direction.


JW

.



Relevant Pages

  • Re: test for "special characters" in text
    ... >I need to make sure that a cell contains NO SPECIAL CHARACTERS (including ... Function foo(s As String, ... own invalid entries that caused the errors. ...
    (microsoft.public.excel.worksheet.functions)
  • [PATCH 2/2] tracing/filters: support for operator reserved characters in strings
    ... Because the operators and parentheses have a higher precedence ... than the operand characters, which is normal, then we can't ... Then after this patch you can still declare string condition like ... support strings with special characters for tracing filters] ...
    (Linux-Kernel)
  • Re: How to "quote" all regexp expression special characters?
    ... the regular expression to use, but such that any regular expression ... special characters are "quoted" to be treated as regular characters. ... What operator or function that takes a string will return the original ... not to quote the "$search4" value. ...
    (comp.lang.perl.misc)
  • Re: special characters treated as text (not trivial I think)
    ... of special characters to a list, ... proc x {nick uhost hand chan text} { ... I think that the error arrising earlier is given of the [string] ...
    (comp.lang.tcl)
  • Re: Re: How to read value of a variable being set in a batch file?
    ... in plain Schulman text, reposting question to the ... Echo the variable in my batch script & then use StdOut.Readall to ... read it into a string -- This works, but I noticed that the string ... contains trailing special characters and I can't seem to figure out ...
    (microsoft.public.scripting.wsh)