Jump to content

[SOLVED] simple preg_match question!


NeoMarine

Recommended Posts

How do I specify in preg_match that I want a match only for:

 

4/ or /4/

 

The first "/" is optional.

 

This is what I've tried:

 

preg_match("/\/?4\/?/", $string)

 

...only the number "4" is found even if it's "54" or "45". I need to say NO characters other than / before or after the number 4.

 

Please do help ::) my mind can't handle this silly simple problem hehe

Link to comment
https://forums.phpfreaks.com/topic/83322-solved-simple-preg_match-question/
Share on other sites

How do I specify in preg_match that I want a match only for:

 

4/ or /4/

 

The first "/" is optional.

 

~(|/)[0-9]~

 

tested:

http://nancywalshee03.freehostia.com/regextester/regex_tester.php?seeSaved=6kkwtc3i

 

*edit woops change that to:

~(|/)[0-9]/~  //I forgot to add the mandatory slash at the end

Ok maybe I confused you a little with my question.

 

What I mean is:

 

$string = "4/5/2/3/4/6/1416/647/4/0/90/"

preg_match("/\/?7\/?/", $string)

 

I want to find ONLY 7 exists in this case. Now, 7 exists in $string but in 647 which I want it to IGNORE. I only want to find it if the string had it in there like this:

 

"8/5/7/" where 7 is between / and / OR if 7 was at the start of the string, it is like this: "7/4/2/" where there's no / in front this time.

That doesn't work correctly.

 

It will find the 7/ in the 647/ substring. I need it to only find either "7/" or "/7/". If there's anything before "7/", such as "647/" then it should return false (no match).

 

Also... when testing it appears to always find true... well... I maybe this is a little more tricky than it seems?

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.