Click to See Complete Forum and Search --> : search and replace


esm
09-08-2003, 12:30 PM
how do I search for the following symbols in the first position of a variable and if not found insert a "=" ?
=, !=, <>, < and >
$category="<32" would be ok (as would >32, <>32, =32 or !=32 )

but if $category="32" then change $category to be"=32"

pyro
09-08-2003, 05:02 PM
Try something like this:

<?PHP
$category = "32" ;
if (!preg_match("/^=|!=|<>|<|>/",$category)) {
$category = "=".$category;
}
echo $category;
?>What the regexp does is check if the variable starts with any of the characters, and if it does not (due to the ! at the beginning of our if statement) it will concatenate a = to the beginning of the string.

esm
09-08-2003, 05:17 PM
you are good! that is why you are a SUPER moderator!!

thanks!:)

pyro
09-08-2003, 05:20 PM
lol... thanks :) I was happy to help...