Jump to content
Seb76

preg_match question

Recommended Posts

$rows[0][2]->val = preg_match("/\b[0-9]{6,6}-[0-9]{4,4}\b/", $rows[0][2]->val)
$rows[0][2]->val = preg_replace("/\b[0-9]{6,6}-[0-9]{4,4}\b/", "Match", $rows[0][2]->val);

Hi there.

 

First question on the forum :-)

I have a working code that uses the preg_replace but I would like to use the preg_match as to simply output a Match/No match

 

 

I experimented with (I don't know why the code block goes on top) but I don't get any result. From what I understand I should get a 1 or 0.

I probably do something wrong?

 

 

 

Share this post


Link to post
Share on other sites

What makes you think you should get back a 1 or 0? The manual says you will get back the updated string.

Share this post


Link to post
Share on other sites

If you want literally "Match" or "No match" then you want preg_match because that will tell you whether the regex matched. Should be obvious when it's phrased that way, right? Since it returns a number for how many times it matched then you'll want to turn that number into your string using whatever your favorite method of doing an if/then/else would be.

Share this post


Link to post
Share on other sites

My bad. Requinex made me re-examine the OP question and I see now that I looked at the examples and assumed they were in time sequence. Mis-applied my knowledge (or lack of!) to the question. Sorry for not being a help...

Share this post


Link to post
Share on other sites

It's ok, I found the reason why it didn't do anything. So silly... I forgot the semi colon at the end...  :tease-01:

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

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.