Using preg_replace to alter youtube links - php

I want to change the standard youtube video links (like: https://www.youtube.com/watch?v=zVzhpkFBFP8) stored in my database to embeded urls (like: https://www.youtube.com/embed/zVzhpkFBFP8) using preg_replace. This is my code so far:
<?php
$Link = getuser($my_id, 'YoutubeLink');
$LinkNew = preg_replace("/watch?v=*/","embed*/","$Link");
echo "$LinkNew" ?>
But it isn't working. I'm probably doing something stupid but I'm new to php so any help is appreciated.

No need to use preg_replace for something like that in which there is no pattern. As watch?v= is always the same, instead use str_replace('watch?v=', 'embed/', $Link);

Your regex as is doesn't work because the ? is a special character in regex so you need to escape it. The question mark makes the preceding character optional. To escape just add a backslash before it \?.
The * also is being used somewhat incorrectly. The asterisk is a quantifier so you are saying zero or more equal signs. If you wanted everything after the equals sign you'd do .*. That would get every other character because . is any character and paired with the * is everything. You don't actually want to do that either because you aren't grouping that and the replace would just delete it. If you were to group that you could use that value later in the replace using $1. Here's a write up on that http://www.regular-expressions.info/brackets.html.
<?php
$Link = getuser($my_id, 'YoutubeLink');
$LinkNew = preg_replace("/watch\?v=/","embed/", $Link);
echo "$LinkNew"; ?>
As #V13Axel has pointed out though this can be done just as easily using str_replace.

Related

Extract text between brakets tags in php using Regex

I have the following content in a string (query from the DB), example:
$fulltext = "Thank you so much, {gallery}art-by-stephen{/gallery}. As you know I fell in love with it from the moment I saw it and I couldn’t wait to have it in my home!"
So I only want to extract what it is between the {gallery} tags, I'm doing the following but it does not work:
$regexPatternGallery= '{gallery}([^"]*){/gallery}';
preg_match($regexPatternGallery, $fulltext, $matchesGallery);
if (!empty($matchesGallery[1])) {
echo ('<p>matchesGallery: '.$matchesGallery[1].'</p>');
}
Any suggestions?
Try this:
$regexPatternGallery= '/\{gallery\}(.*)\{\/gallery\}/';
You need to escape / and { with a \ before it. And you where missing start and end / of the pattern.
http://www.phpliveregex.com/p/fn1
Similar to Andreas answer but differ in ([^"]*?)
$regexPatternGallery= '/\{gallery\}([^"]*?)\{\/gallery\}/';
Don't forget to put / at the beginning and the end of the Regex string. That's a must in PHP, different from other programming languages.
{,},/ are characters that can be confused as a Regex logic, so you have to escape it using \ like \{.
Use ? to make the string to non-greedy, thus saves memory. It avoids error when facing this kind of string "blabla {galery}you should only get this{/gallery} but you also got this instead.{/gallery} Rarely happens but be careful anyway".
Try this RegEx:
\{gallery\}(.*?)\{\/gallery\}
The problem with your RegEx was that you did not escape the / in the closing {gallery}. You also need to escape { and }.
You should use .*? for a lazy match, otherwise if there are 2 tags in one string, it will combine them. I.e. {gallery}by-joe{/gallery} and {gallery}by-tim{/gallery} would end up as:
by-joe{/gallery} and {gallery}by-tim
However, using a lazy match, you would get 2 results:
by-joe
by-tim
Live Demo on Regex101

Trim URL by variable

Hi I have a Link like this:
mypage.php?product=3&page=1
I want to delete the &page=1, &page=2, &page=5 and so.
I have tried this, but I think it is not right.
str_replace('/(\\?|&)page=.*?(&|$)/', '', $link);
Thanks for your help.
str_replace() doesn't work with regular expressions, so you'd use preg_replace() instead:
$url = preg_replace('/[?&]page=[^&]+/', '', $url);
Two changes here: first, it's better to use a character class instead of alternation when you target individual symbol only (not having to escape ? within the brackets is a nice bonus), second, [^&]+ ('match any number of non-& characters') construct is more direct and readable than .+?(&|$) one.

Simple regex replace regardless of middle text php

say I have this
searchpage-20/11111111111?node=15
how would I setup a regex to replace the entire string without worrying about the
11111111111
in the middle as long as the rest matches.
I tried
searchallmp3-20/(.+?)\?node=
You should post the code, to see how you are calling the regex and you need also to describe, what is not working.
I assume you are getting some error message because you are using / without escaping it in your regex and your delimiter is also the /
Two possibilities:
Escape the /
/searchallmp3-20\/(.+?)\?node=/
Use another delimiter
~searchallmp3-20/(.+?)\?node=~
See Delimiters on php.net
I still don't get the replace part of your question. If you want to remove the digits before the ?, you should capture the other parts of the string
~(searchallmp3-20/).+?(\?node=~)
and replace with
`$1$2`
this will result in
searchpage-20/?node=15
Is it this what you want?
How about
<?php
$search = 'searchpage-20/11111111111?node=15';
$reg = '#(searchpage-\d+/)\d+(\?node=\d+)#';
echo preg_replace($reg, '${1}blah${2}', $search);
OUTPUT
searchpage-20/blah?node=15
You should be able to use this:
/searchpage-(\d+)\/(\d+)\?node=(\d+)/
Example usage:
preg_replace('/searchpage-(\d+)\/(\d+)\?node=(\d+)/', '', 'searchpage-20/11111111111?node=15');

Dreamweaver Regex Find and Replace Using Regular Expression

I am using a Regular Expression to perform a find and replace with dreamweaver. I am running into some difficulty. This is what I have in my page (note that there is a syntax error because I need an additional parenthesis at the end of the string).
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']);
I am trying to performa a find and replace that will produce this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']));
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
This is what I am using to perform the find. It seems to be replacing too much text (it starts with the $_POST from the $email variable, but continues all the way down to the $_POST for the $name variable)
Find: \$_POST['([^<]*)']
Replace: $_POST['$1'])
I end up with this:
$email=htmlspecialchars(mysql_real_escape_string($_POST['email']);
$name=htmlspecialchars(mysql_real_escape_string($_POST['name']));
As you can see, it only fixes the last instance (this is because the find function is selecting both lines from $_POST['email'] all the way to $_POST['name']). Any ideas on how to fix this? Thank you!
Add a question mark to make it non-greedy. Also, you need to escape the [ and ] characters that you want to match.
Find: \$_POST\['([^<]*?)'\]
Replace: $_POST['$1'])
Or, alternatively, user a ' character instead of a < character to match the value within the quotes:
Find: \$_POST\['([^']*)'\]
Replace: $_POST['$1'])

Regular expression anchor text for a link

I am trying to pull the anchor text from a link that is formatted this way:
<h3><b>File</b> : i_want_this</h3>
I want only the anchor text for the link : "i_want_this"
"variable_text" varies according to the filename so I need to ignore that.
I am using this regex:
<a href=\"\/en\/browse\/file\/variable_text\">(.*?)<\/a>
This is matching of course the complete link.
PHP uses a pretty close version to PCRE (PERL Regex). If you want to know a lot about regex, visit perlretut.org. Also, look into Regex generators like exspresso.
For your use, know that regex is greedy. That means that when you specify that you want something, follwed by anything (any repetitions) followed by something, it will keep on going until that second something is reached.
to be more clear, what you want is this:
<a href="
any character, any number of times (regex = .* )
">
any character, any number of times (regex = .* )
</a>
beyond that, you want to capture the second group of "any character, any number of times". You can do that using what are called capture groups (capture anything inside of parenthesis as a group for reference later, also called back references).
I would also look into named subpatterns, too - with those, you can reference your choice with a human readable string rather than an array index. Syntax for those in PHP are (?P<name>pattern) where name is the name you want and pattern is the actual regex. I'll use that below.
So all that being said, here's the "lazy web" for your regex:
<?php
$str = '<h3><b>File</b> : i_want_this</h3>';
$regex = '/(<a href\=".*">)(?P<target>.*)(<\/a>)/';
preg_match($regex, $str, $matches);
print $matches['target'];
?>
//This should output "i_want_this"
Oh, and one final thought. Depending on what you are doing exactly, you may want to look into SimpleXML instead of using regex for this. This would probably require that the tags that we see are just snippits of a larger whole as SimpleXML requires well-formed XML (or XHTML).
I'm sure someone will probably have a more elegant solution, but I think this will do what you want to done.
Where:
$subject = "<h3><b>File</b> : i_want_this</h3>";
Option 1:
$pattern1 = '/(<a href=")(.*)(">)(.*)(<\/a>)/i';
preg_match($pattern1, $subject, $matches1);
print($matches1[4]);
Option 2:
$pattern2 = '()(.*)()';
ereg($pattern2, $subject, $matches2);
print($matches2[4]);
Do not use regex to parse HTML. Use a DOM parser. Specify the language you're using, too.
Since it's in a captured group and since you claim it's matching, you should be able to reference it through $1 or \1 depending on the language.
$blah = preg_match( $pattern, $subject, $matches );
print_r($matches);
The thing to remember is that regex's return everything you searched for if it matches. You need to specify that only care about the part you've surrounded in parenthesis (the anchor text). I'm not sure what language you're using the regex in, but here's an example in Ruby:
string = 'i_want_this'
data = string.match(/<a href=\"\/en\/browse\/file\/variable_text\">(.*?)<\/a>/)
puts data # => outputs 'i_want_this'
If you specify what you want in parenthesis, you can reference it:
string = 'i_want_this'
data = string.match(/<a href=\"\/en\/browse\/file\/variable_text\">(.*?)<\/a>/)[1]
puts data # => outputs 'i_want_this'
Perl will have you use $1 instead of [1] like this:
$string = 'i_want_this';
$string =~ m/<a href=\"\/en\/browse\/file\/variable_text\">(.*?)<\/a>/;
$data = $1;
print $data . "\n";
Hope that helps.
I'm not 100% sure if I understand what you want. This will match the content between the anchor tags. The URL must start with /en/browse/file/, but may end with anything.
#(.*?)#
I used # as a delimiter as it made it clearer. It'll also help if you put them in single quotes instead of double quotes so you don't have to escape anything at all.
If you want to limit to numbers instead, you can use:
#(.*?)#
If it should have just 5 numbers:
#(.*?)#
If it should have between 3 and 6 numbers:
#(.*?)#
If it should have more than 2 numbers:
#(.*?)#
This should work:
<a href="[^"]*">([^<]*)
this says that take EVERYTHING you find until you meet "
[^"]*
same! take everything with you till you meet <
[^<]*
The paratese around [^<]*
([^<]*)
group it! so you can collect that data in PHP! If you look in the PHP manual om preg_match you will se many fine examples there!
Good luck!
And for your concrete example:
<a href="/en/browse/file/variable_text">([^<]*)
I use
[^<]*
because in some examples...
.*?
can be extremely slow! Shoudln't use that if you can use
[^<]*
You should use the tool Expresso for creating regular expression... Pretty handy..
http://www.ultrapico.com/Expresso.htm

Categories