I want to change the id partner=852501 to partner=985412 in my code, but stuck in this step. Example:
http://a-ads.com/?partner=852501
My attempt:
([1-z0-9\/]+)\/\/(a-ads\.com\/?)
How to do the right thing?
Regex: partner=\K[^&]+
Details:
\K resets the starting point of the reported match
[^&]+ Match a single character not present in the list & one and unlimited times
PHP code:
preg_replace('/partner=\K[^&]+/', '985412', $url);
preg_replace you can replace the value
$string = 'http://a-ads.com/?partner=852501';
$your = 985412;
echo preg_replace('/(\d+)/', $your, $string);
UPD: The value can be obtained with the help of preg_match
preg_match('/partner=(\d+)/', $string, $match);
echo $match[1];
Related
Dot (.) Meta character is not matching text and showing zero(o) output. Please tell me what i am missing in this code.
$string = 'pakistan';
echo preg_match('/p.n/',$string);
The following regex:
$string = 'pakistan';
echo preg_match('~^p.+n$~',$string);
Anchors the string to the beginning and end and requires at least one (but unlimited times) character between p and n.
Use below code:-
preg_match("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
If you want to count how many times specific word is repeating in the string
Use preg_match_all
preg_match_all ("/p.*n/U", $string, $match);
echo '<pre>'; print_r($match[0]);
Hope it will help you :)
i need to explode youtube url from this line:
[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]
It is possible? I need to delete [embed] & [/embed].
preg_match is what you need.
<?php
$str = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
preg_match("/\[embed\](.*)\[\/embed\]/", $str, $matches);
echo $matches[1]; //https://www.youtube.com/watch?v=L3HQMbQAWRc
$string = '[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]';
$string = str_replace(['[embed]', '[/embed]'], '', $string);
See str_replace
why not use str_replace? :) Quick & Easy
http://php.net/manual/de/function.str-replace.php
Just for good measure, you can also use positive lookbehind's and lookahead's in your regular expressions:
(?<=\[embed\])(.*)(?=\[\/embed\])
You'd use it like this:
$string = "[embed]https://www.youtube.com/watch?v=L3HQMbQAWRc[/embed]";
$pattern = '/(?<=\[embed\])(.*)(?=\[\/embed\])/';
preg_match($pattern, $string, $matches);
echo $match[1];
Here is an explanation of the regex:
(?<=\[embed\]) is a Positive Lookbehind - matches something that follows something else.
(.*) is a Capturing Group - . matches any character (except a newline) with the Quantifier: * which provides matches between zero and unlimited times, as many times as possible. This is what is matched between the groups prior to and after. This are the droids you're looking for.
(?=\[\/embed\]) is a Positive Lookahead - matches things that come before it.
I'm trying to get "content" from this string:
$string = "start start content end";
with preg_match like this:
preg_match('/start(.*?)end/', $string, $matches);
echo $match[1];
but, problem is $matches[1] returns start content not only content because there are two start in $string (and maybe more)
How to get only content part with preg_match?
Using a negative lookahead:
$string = "start start content end";
preg_match('/start\h*((?:(?!start).)*)end/', $string, $matches);
echo $matches[1];
// content
(?:(?!start).) will match any character if that is not followed by start.
You can add + to eat all start and then capture your required string.
(?:start\s)+(.+?)end
i am using php Regular expressions but i can't retrieve values that i group using ()
this is my input
<img src="http://www.example.com/image.jpg" title="title" />
i need only src value , this is my regex '"<img src=\"(.*?)\".*?\/>"'
if i can retrieve First group just like java patterns my problem is sloved
preg_match_all('"<img src=\"(.*?)\".*?\/>"', $source, $re);
print_r($re);
and it return full image tag like this <img src="http://www.example.com/image.jpg" title="title" />
To match a single string, preg_match function is enough. You don't need to go for preg_matchall function. If you want to match more number of strings then you could use preg_matchall function. And also first try to match the exact string through the pattern rather than to go for grouping. If it's impossible to match a particular string then go for grouping.
In the below, matching the exact value of src attribute is done.
You could get the value of src in two ways,
1. positive lookbehind
Regex:
(?<=src=\")[^\"]*
PHP code:(Through match_all)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~(?<=src=\")[^\"]*~';
preg_match_all($regex, $string, $matches);
print_r($matches);
?>
PHP code:(Through match)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~(?<=src=\")[^\"]*~';
if (preg_match($regex, $string, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> http://www.example.com/image.jpg
Explanation:
(?<=src=\") Positive look-behind is used here. So the regex engine puts the match marker just after to the src=".
[^\"]* Now it starts matching any character zero or more times but not of ". When it finds a ", it stops matching characters.
2. Using \K
Regex:
src=\"\K[^\"]*
PHP code (through match)
<?php
$string = "<img src=\"http://www.example.com/image.jpg\" title=\"title\" />";
$regex = '~src=\"\K[^\"]*~';
if (preg_match($regex, $string, $m)) {
$yourmatch = $m[0];
echo $yourmatch;
}
?> //=> http://www.example.com/image.jpg
Explanation:
\K resets the starting point of the reported match. Any previously consumed characters are no longer included in the final match.
src=\"\K So it discards the previously matched src=".
[^\"]* Matches any character zero or more times but not of "
You're using preg_match_all so that you need to pass index as well, use print_r($re[1]); to get results.
I Got It Accidently !
we can Code Like this for first Grouped
print_r($re[1]);
<?php
$string = "http://example.com/file/D1 http://example.com/file/D2
http://example.com/file/D3";
preg_match_all('/(https?\:\/\/)?(www\.)?example\.com\/file\/(\w+)/i', $string, $matches);
foreach($matches[3] as $value)
{
print $value;
}
?>
I want to preg match the third link and get "D3".
I dont want that it matches with the other two links. This is why it should check if the link has a whitespace at the beginning or the end.
I know that to match with whitespace the expression is \s. I tried but somehow I don't get it. :(
You can add the $ to match the end of the string like this, and it will only return the last one.
preg_match_all('/(https?\:\/\/)?(www\.)?example\.com\/file\/(\w+)$/i', $string, $matches);