Jump to content

[SOLVED] Help me with my preg_match plz


physaux

Recommended Posts

Does anyone see anything wrong with this:

 

			
preg_match('/animal\/([^<]+)">SALLY/', $contents, $match); 
echo $match;

 

The string that i would be reading would be like this:

ef="/animal/cdj87sd3">SALLY</a>

 

Where the begining is

animal/

and the end is

">SALLY

and the characters in the middle are lowercase letters and numbers, limit of 8 characters

I want the variable $match to have those 8 random characters.

 

could you please help me! Ty!!

Link to comment
Share on other sites

Try this...

 

preg_match('~animal/([a-z0-9]+){1,8}">SALLY~', $contents, $match);
echo $match[1];

 

Note the 1,8 part means to make that section between 1 and 8 characters, if theres a more specific requirement just change those numbers.

Link to comment
Share on other sites

Ok thanks, but I am just wondering, in your example you did not "escape" the "/" after "animal"

 

Did you miss that, or are we not supposed to do that. Because I thought i read that you have to "escape" it with a "\"

 

But im new at regex, so just checking

 

thanks!

Link to comment
Share on other sites

In regular expressions you only have to escape the forward slash if that is the character you choose to use for the delimiters (the start and end character). Since you are working with a path (at least what looks like one) using the forward slash for the delimiter is not really a good idea as you'll have to do alot of escaping. I simply use the tilde (~) character as the delimiter instead.

Link to comment
Share on other sites

In regular expressions you only have to escape the forward slash if that is the character you choose to use for the delimiters (the start and end character). Since you are working with a path (at least what looks like one) using the forward slash for the delimiter is not really a good idea as you'll have to do alot of escaping. I simply use the tilde (~) character as the delimiter instead.

 

ahhh true true I forgot you can change the delimiter. Smart. Thanks for the help!!!

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.