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
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

Link to comment
Share on other sites

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.

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.