Forum Moderators: coopster

Message Too Old, No Replies

Does PHP have equivalent of $string ~= /char/ ?

Check string for invalid character.

         

cochranrg

7:42 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Need to check a string in PHP for an invalid character. In Perl I would use:

if ($string ~= /^/) {

How can I do this with PHP?

Thanks in advance,
Rob

dreamcatcher

8:03 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What do you mean by invalid char?

You can use the strstr [uk2.php.net] function if you want to check for an instance of something in a string.

$string = 'Boo^';

To check for the caret above you would do:

if (strstr($string,'^'))
{
return true;
}
else
{
return false;
}

strstr returns true if the specified char is found and false if not.

Or isn`t that what you meant?

dc

zCat

8:05 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



if ($string ~= /^/) {

preg_match
and friends are what you are looking for, see e.g.

[php.net...]

Note that PHP's regexes are similar to, but not quite like Perl's, which can sometimes cause confusion.

Personally I find Perl much less tiresome to work with when it comes to regular expressions

cochranrg

8:28 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



Hmmm, I can't get either of these suggestions to work. Tried strstr, strpos, and preg_match. No love.

Anyway, have to play around with it later.

Thanks for the suggestions.

Rob

jatar_k

10:47 pm on Apr 4, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



take a look at this thread, it mentions escaping the ^
[webmasterworld.com...]

zCat

11:30 pm on Apr 4, 2006 (gmt 0)

10+ Year Member



take a look at this thread, it mentions escaping the ^

There's one of those subtle differences: in Perl, a single ^ is treated as a literal (though it would be better practice to escape it in any case).

cochranrg

6:43 pm on Apr 5, 2006 (gmt 0)

10+ Year Member



Hi Jatar, yeah that thread was one of mine too!

Never could get this to work, so reformatted my text as tab delimited and split on the /\t/ and newline /\n/. Couldn't get it to work earlier but from what you helped me with I can now!

Still couldn't figure out how to tell if a string has a ^ or a ~ in it, but it doesn't matter anymore.

Thanks for all the help.

Rob

jatar_k

6:44 pm on Apr 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> was one of mine too

sry, I didn't even notice, hehe, funny though