php preg_match replace result duplicate - php

I am a newbie in preg_match patterns, so I would be glad if someone could help me for next situation:
I need to replace those string:
[popup="about"]about me[/popup]
to
<a href="#PopupAbout" data-plugin-options='{"type":"inline", preloader: false}'>about me</a>
I have tried with $pattern = '/\[popup="(.*?)"\](.*?)\[\/popup\]/'; but it does not give me expected result, it give duplicated results. And how can I replace it all in a simple way!
Regards!

How about:
$str = preg_replace('~\[popup="about"\](.+?)\[/popup\]'~,
"<a href=\"#PopupAbout\" data-plugin-options='{\"type\":\"inline\", preloader: false}'>$1</a>",
$str);

Try this:
preg_match("/\[popup="(.*)"\](.*?)\[\/popup\]/", $input_line, $output_array);
I get this result:
Array
(
[0] => [popup="about"]about me[/popup]
[1] => about
[2] => about me
)
You can test it online here: http://www.phpliveregex.com/

Related

Simple task regex expression replacing any "%function test%" by test

I'm trying to find the good expression to replace any string starting with :
"%function
and ending by :
%"
by the inbetween string for example :
"%function test%" should return
test
and "%function test%","% function test%" should return :
test,test
I tried this
preg_replace('/"%function (.*)?%"/', '$1',$string);
I had great hope at first, coz my first example work great but with multiple function not so great.
For more information you can see the code and try out here (a sort of phpfiddle): http://sandbox.onlinephpfunctions.com/code/00232644b801271e2ffb2ddf4cd450ddffcb50c2
Any help would be greatly appreciated,
best regards.
By using ? outside the capturing group, you're making it optional. Instead what you want is a non-greedy * so you'll have to do. Other choice is to do ([^%]+)
preg_replace('/"%function (.*?)%"/', '$1',$string);
You can use look arounds
(?<="%function )[^%]+(?=%")
Regex Demo
preg_match_all( "/(?<=\"%function )[^%]+(?=%\")/", "\"%function test%\"", $matches);
Will produce
Array ( [0] => Array (
[0] => test
)
)
%\s*\S+\s+(\S+?)\s*%
Try this.Replace by $1.See demo.
https://regex101.com/r/wZ0iA3/8
$re = "/%\\s*\\S+\\s+(\\S+?)\\s*%/im";
$str = "%function test%\n%function test%,% function test%";
$subst = "$1";
$result = preg_replace($re, $subst, $str);

preg_match on special characters

I’m trying to make a preg_match in my PHP code, but I can't seem to get it to work.
I have these 3 tags
{$success url='http://www.domain.localhost/form-success'}
{$error url='http://www.domain.localhost/form-error'}
{$button title='send form'}
How can I get my preg_match to accept what I’m trying to do?
I’m trying to get {$button(*)} out to my match path, and the same I want to do with the 2 others {$success(*)} and {$error(*)}
Now I think it should look like this
preg_match("/\{\$button(.+?)}/s", $content, $match);
But it still isn’t working, so I hope other people can help me here.
\$ already means "literal $" in PHP strings, so when put in a regex it just means "end of string".
Try \\\$ instead.
I think the correct regex to retrieve the 'button' tag should be : \{\$button([^\}]*)\}
You can try your expression on http://regexpal.com/
So with php :
preg_match("/\{\$button([^\}]*)\}/s", $content, $match );
You need to escape the closing } and need to use preg_match_all to match all the lines.
Try this regex:
preg_match('/\{\$(?:success|error|button)\s+([^}]+)\}/i', $content, $match );
Working Demo: http://ideone.com/fKq5L2
OUTPUT:
Array
(
[0] => {$success url='http://www.domain.localhost/form-success'}
[1] => {$error url='http://www.domain.localhost/form-error'}
[2] => {$button title='send form'}
)

Preg_Match_All and RegEx Getting Values

I'm trying to get the 2 values in this string using regex:
a:2:{i:45;s:29:"Program Name 1";i:590;s:19:"Program Name 2";}
There are 2 variables that start with "s:" and end with ":" which I am attempting to get from this string (and similar strings.
$string = 'a:2:{i:45;s:29:"Program Name 1";i:590;s:19:"Program Name 2";}';
preg_match_all("/s:(\d+):/si", $page['perfarray'], $match);
print_r($match);
I have tried numerous things but this is the first time I've attempted to use regex to get multiple values from a string.
This is the current result: Array ( [0] => Array ( ) [1] => Array ( ) )
Any constructive help is greatly appreciated. I have already read the functions on php.net and I can't find a similar question on stack overflow that matches my needs closely enough. Thanks in advance.
That looks like a serialized string. Instead of using a regular expression, use unserialize() to retrieve the required value.
Update: It looks like your string is not a valid serialized string. In that case, you can use a regular expression to get the job done:
$string = 'a:2:{i:45;s:29:"Program Name 1";i:590;s:19:"Program Name 2";}';
if(preg_match_all("/s:(\d+):/si", $string, $matches)) {
print_r($matches[1]);
}
Output:
Array
(
[0] => 29
[1] => 19
)
That should work:
preg_match_all("/s:([0-9]+):/si", $page['perfarray'], $match);

Is there a way to match recursively/nested with regex? (PHP, preg_match_all)

How can I match both (http://[^"]+)'s?:
(I know it's an illegal URL, but same idea)
I want the regex to give me these two matches:
1 http://yoursite.com/goto/http://aredirectURL.com/extraqueries
2 http://aredirectURL.com/extraqueries
Without running multiple preg_match_all's
Really stumped, thanks for any light you can shed.
This regular expression will get you the output you want: ((?:http://[^"]+)(http://[^"]+)). Note the usage of the non-capturing group (?:regex). To read more about non-capturing groups, see Regular Expression Advanced Syntax Reference.
<?php
preg_match_all(
'((?:http://[^"]+)(http://[^"]+))',
'',
$out);
echo "<pre>";
print_r($out);
echo "</pre>";
?>
The above code outputs the following:
Array
(
[0] => Array
(
[0] => http://yoursite.com/goto/http://aredirectURL.com/extraqueries
)
[1] => Array
(
[0] => http://aredirectURL.com/extraqueries
)
)
you can split the string with this function:
http://de.php.net/preg_split
each part can contain e.g. one of the urls in the array given in the result.
if there is more content maybe call the preg_split using a callback operation while your full text is "worked" on.
$str = '';
preg_match("/\"(http:\/\/.*?)(http:\/\/.*?)\"/i", $str, $match);
echo "{$match[0]}{$match[1]}\n";
echo "{$match[1]}\n";

Why is this regular expression not working?

Content of 1.txt:
Image" href="images/product_images/original_images/9961_1.jpg" rel="disable-zoom:false; disable-expand: false"><img src="im
Code that does not work:
<?php
$pattern = '/(images\/product_images\/original_images\/)(.*)(\.jpg)/i';
$result = file_get_contents("1.txt");
preg_match($pattern,$result,$match);
echo "<h3>Preg_match Pattern test:</h3><br><br><pre>";
print_r($match);
echo "</pre>";
?>
I expect this result:
Array
(
[0] => images/product_images/original_images/9961_1.jpg
[1] => images/product_images/original_images/
[2] => 9961_1
[3] => .jpg
)
But i take this-like:
Array
(
[0] => images/product_images/original_images/9961_1.jpg" rel="disable-zoom:false; disable-expand: false">
[1] => images/product_images/original_images/
[2] => 9961_1.jpg" rel="disable-zoom:false; disable-expand: false">
)
I'n tired of trying from a million combinations of this regexp. I dunno what's wrong. Please and thanks a lot!
Make it ungreedy:
$pattern = '/(images\/product_images\/original_images\/)(.*?)(\.jpg)/i';
Remember that Regular Expressions are greedy. Your second capture (.*) says to match any character except the new line (unless in mutliline mode). So it is probably capturing the rest of the line.
You can make it ungreedy as suggested by Wrikken. But I like to ensure I am capturing what I want. In your case, it looks like the value of the href attribute. So really I want at least 1 character, can't be a quote, followed by the jpg extension:
$pattern = '/(images\/product_images\/original_images\/)([^'"]+)(\.jpg)/i';
Here's the basic regex:
href="((.*/)(.*?)(.jpg))"
Do not parse HTML with regex.
Do not parse HTML with regex.
Do not parse HTML with regex.

Categories