0

I want to change this sentence:

<iframe width="850" height="478" src="https://www.youtube.com/embed/4zH9Zca1vRM" frameborder="0" allowfullscreen></iframe>

To this:

https://www.youtu.be/4zH9Zca1vRM

I can do it for every iframe of youtube video in the database. What is the right sentence for do it? I gess something like this:

step 1. Replacing first part:

wp search-replace '<iframe width="*" height="*" src="https://www.youtube.com/embed/' 'https://www.youtu.be/'  --regex

step 2. Replacing last part:

wp search-replace '" frameborder="0" allowfullscreen></iframe>' ''  --regex

Is it right? I'm not sure about quotes and wildcards.

Thank you!

blizzrdof77
  • 363
  • 4
  • 14
aitor
  • 2,281
  • 3
  • 22
  • 44

3 Answers3

1

To update with regular expressions, be sure to escape all the forward slashes for the first argument:

$ wp search-replace '<iframe width="([0-9]+)" height="([0-9]+)" src="https:\/\/www.youtube.com\/embed\/([a-zA-Z0-9]+)" frameborder="0" allowfullscreen><\/iframe>' 'https://www.youtu.be/\3' --regex 

Disregard the first two matched, since they're not important and use third match in the second (replacement) argument.

Also remember to:

$ wp cache flush

It's a good idea to do a --dry-run first to see if there are any tables affected that you might not intend to modify.

http://wp-cli.org/commands/search-replace/

DanielV
  • 86
  • 4
0

You won't need regex for this, you can quote it with a single tick. You can run this to test and show the replacements that would occur. Remove the --dry-run flag to make the actual replacements.

$ wp search-replace '<iframe width="850" height="478" src="https://www.youtube.com/embed/4zH9Zca1vRM" frameborder="0" allowfullscreen></iframe>' 'https://www.youtu.be/4zH9Zca1vRM' --dry-run
  • Yes, it works for this case, but does not work with wildcards: width="[wildcard here]" height="[wildcard here]" thank you! – aitor Apr 03 '16 at 10:18
0

yet an other regex, this one fixes youtube links or iframe http or https

and remplaces it by a simple string that will display as embed in wordpress:

https://youtu.be/gVoUPnXqvNE


$ wp search-replace '<(?:a|iframe)\s[^>]*(?:href|src)="https?:\/\/www\.youtube\.com\/(?:embed\/|watch\?v=)([a-zA-Z0-9-]+)(?:(?:\?&)[^"#\s]*)?(#t=[0-9]+)?"[^>]*>.*?<\/(?:a|iframe)>' 'https://youtu.be/\1\2' --regex --regex-flags='s'

Antony Gibbs
  • 1,321
  • 14
  • 24