Jump to content

[SOLVED] Just can't see why!


Adam

Recommended Posts

Hey guys, got a regex problem!

 

I'm trying to return: (...)/content/what_ever/may_be/here from a URL.

 

At the moment I'm using:

 

preg_match('/\/content\/[.]+$/', $url, $matches);

 

.. but having little success! Can anyone see where I'm going wrong? At the moment just returns an empty array ???

 

Thanks for your help,

Adam

Link to comment
https://forums.phpfreaks.com/topic/150902-solved-just-cant-see-why/
Share on other sites

Ah got it partly working now using:

 

/\/content\/.+$/

 

... but this returns "/content/" as well.

 

"/content/" must be in the string, but I don't want to return it. I could strip out "/content/" afterward, but I'd prefer to just do this in the regular expression, if that's possible?

 

Thanks,

Adam

If you don't want to include the /content/, you can do a capture of everything else, and simply use that.

 

preg_match(#/content/(.+)$#, $string, $match)

 

What ever comes after the /content/ will be stored into $match[1], which saves you from needing to strip /content/ out afterwards. EDIT - Of course, change $string to the name of the variable in question that you are using.

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.