1

I am running a code which takes a youtube url as input and i am using str_replace on it.

$title = str_replace('watch?v=', 'embed/', $title);

but the replacement just wont happen. I keep getting the same string back.

The youtube url im using as of now is http://www.youtube.com/watch?v=iwQx9gw2NfM

what mistake am I making ? The the serch string is CLEARLY present in the url but it wont get replaced.

Or is there a better way to do this ?

EDIT: The code works fine... I was trying to replace the wrong variable

4
  • 1
    Why do you hold the url in a variable called $title ? -- And otherwise, please show a bin2hex() output of its content. Commented Dec 17, 2011 at 20:34
  • 1
    I copied your code exactly as it appears and the example URL you gave returns http://www.youtube.com/embed/iwQx9gw2NfM Commented Dec 17, 2011 at 20:34
  • are you sure you are not re-assigning the original value back to $title after the replacement has taken place? There is no reason the code you pasted should not work. Please add more of the surrounding code. Commented Dec 17, 2011 at 20:38
  • @mario Thank you man ! Ive been trying to replace the wrong variable...! Just wasted like 30 mins doing nothing Commented Dec 17, 2011 at 20:38

1 Answer 1

5

This code works fine:

$title = "http://www.youtube.com/watch?v=iwQx9gw2NfM";

$title = str_replace('watch?v=', 'embed/', $title);

echo $title;  //=> http://www.youtube.com/embed/iwQx9gw2NfM

See it work here on tehplayground

You must have another mistake in your code. Please paste your context code for additional help.

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.