Jump to content

preg match array


astralaaron

Recommended Posts

can anyone point out why I get this error from this code?or tell me if my code makes no sense at all please.

<?phparray match("/red","/yellow","/blue");array color("<span style=\"color:red\">","<span style=\"color:yellow\">","<span style=\"color:blue\">");$string = '/red Hello';if (preg_match('/^'.match().'/', $string)){echo str_replace(''.match().'', ''.color().'', $string.'</span>');} else {echo 'error';}?>

Link to comment
Share on other sites

It doesn't make sence at all... what were you thinking? No, seriously... what were you trying to do?

Link to comment
Share on other sites

It doesn't make sence at all... what were you thinking? No, seriously... what were you trying to do?
haha, i wanted when a string was typed in with somekind of code at the beginning like /r or /redit will output red text.I was trying to use an array to match the /code with a span with red colorive always been confused by arrays as you can see :]
Link to comment
Share on other sites

Well, first stop, arrays are defined as

$varName = array('/red', '/yellow', '/blue');

where $varName is the name of the array and can be anything you want.Second, there's no match() function. I suppose you were trying to target the match array. Even so, regular expression don't work on arrays. You need to check each array value individually (while looping over the array).Third, where do you receive the input from anyway? Nowhere, just hard coded? Well then, how about:

echo '<span style="color:', substr($varName[0], 1), ';">Text that will change colors</span>';

That will get whatever the first member of the $varName array (as I've called it above) is, and make the span have that color.

Link to comment
Share on other sites

this is what I came up with and it works fine

<?php$check   = array('@red','@yellow','@blue','@green','@orange');$against = array("<span style=\"color:red\">","<span style=\"color:yellow\">","<span style=\"color:blue\">","<span style=\"color:green\">","<span style=\"color:orange\">");$txt = '@green This text should be be green';$string = $txt.'</span>';foreach ($check as $id => $value) {	if(preg_match('/'.$value.'/',$string)){	 	 $write = str_replace($value,$against[$id],$string);	 echo($write);	}}?>

what I was really trying to do was have /red /yellow / blue etc but it was thinking i was trying to use /r /y /b (modifiers that don't exist)i tried adding an escape character but then it would not match it and it would echo out /red text instead of <span style=\"color:red\"> txt

Link to comment
Share on other sites

You are right, this works:

<?php$check   = array('/red','/yellow','/blue','/green','/orange');$against = array("<span style=\"color:red\">","<span style=\"color:yellow\">","<span style=\"color:blue\">","<span style=\"color:green\">","<span style=\"color:orange\">");$txt = '/red This text should be be red';$string = $txt.'</span>';$write = str_replace($check,$against,$string);echo($write);?>

the one i had above worked but just not with a /just to know.. can anyone tell me how to pregmatch a / ?

Link to comment
Share on other sites

<?php   $check   = array('@red','@yellow','@blue','@green','@orange');   $against = array("<span style=\"color:red\">","<span style=\"color:yellow\">","<span style=\"color:blue\">","<span style=\"color:green\">","<span style=\"color:orange\">");   $txt = '@green This text should be be green';   $string = $txt.'</span>';   $string = str_replace($check, $against, $string);   echo $string;?>

Link to comment
Share on other sites

Use a different delimiter for the regex. Most people seem to use / as a delimiter for their patterns./<pattern>/The actual delimiter is competely up to you, just use one that isn't going to appear in the rest of the pattern.@<pattern>@#<pattern>#etcIf you need to use the delimiter inside the pattern, then you just escape it the same way you escape anything else.

Link to comment
Share on other sites

Use a different delimiter for the regex. Most people seem to use / as a delimiter for their patterns./<pattern>/The actual delimiter is competely up to you, just use one that isn't going to appear in the rest of the pattern.@<pattern>@#<pattern>#etcIf you need to use the delimiter inside the pattern, then you just escape it the same way you escape anything else.
thanks, i tried to just escape it.. but it wouldn't work out? I'll try a different delimiter
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...