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
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.