1

This is my if statement that will take this example href from the source code:

href="/toronto/2013/05/14/the-airborne-toxic-event/4296/" 

from the source code

if ($text=preg_match('/href="([^"]*)"/', $text, $matches))
{
    $output=$matches[1];
    return $output;
}

and return

/toronto/2013/05/14/the-airborne-toxic-event/4296/

I am trying to return that url without either "/toronto" or "/toronto/" (I'm not sure which one I will need)

If you could show me the regex expression that would do that I would really appreciate it. Thanks

1
  • 1
    Consider using str_replace instead of a regular expression. This use case is simple enough that it's not really needed. Commented May 14, 2013 at 15:23

1 Answer 1

1

Use preg_replace:

return preg_replace('~^/?toronto/~', '', $output);

If you're sure that "toronto/" does not appear anywhere else in the string, you can use str_replace:

return str_replace('/toronto', '', $output);

This assumes there'll always be a leading slash.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.