echo preg_replace("#\[map\](.+)\[\/map\]#e", '[link=maps.php/".$map_id[array_search("$1", $map_name)]."/$1/]$1[/link]', $text);
is my try.
I want to replace
[map]map_name[/map]
to
[link]maps.php/id/map_name[/link]
I have two arrays, $map_id and $map_name. They contain the exact same items and items are connected with the same key, eg: $map_id[123] that ID is for that map: $map_name[123].
My preg_replace does not work as it returns: Failed evaluating code: [link=maps.php/".in_array("ksz_luminous", $map_name)."/ksz_luminous/]ksz_luminous[/link]
You should not use the e flag. Suppose I used this:
[map]",array())].shell_exec("evil command of evil").$map_id[array_search("[/map]
Or something like that.
Anyway, try this:
echo preg_replace_callback("(\[map\](.+?)\[/map\])i",
function($m) use ($map_id,$map_name) {
return "[link=maps.php/"
.$map_id[array_search($m[1], $map_name)]
."/".$m[1]."/]".$m[1]."[/link]";
}, $text);
Related
I'd like to check each time a string contains a link and then extract it.
example:
'Check this out… https://www.link.co… and also check this out https://www.link2.co'
I know how to check if a string contains a substring ONCE using:
if (strpos($request_content, '!test') === 0) {}
however I dont know how to check each time and then extract that.. in this case i'd like to extract each link by finding the pattern http
so in the above example id like to output both links as variables like
var1 - https://www.link.co
var2 - https://www.link2.co
How can I accomplish this? Thank you!
The key in here is "pattern" as you said. In php, there is a function that has called "preg_replace" which basicly checks a pattern in your string and replaces it with something else you specified. So the code needs to be like this:
/*----- Define your string in here ------*/
$my_text = 'Check this out… https://www.link.co… and also check this out https://www.link2.co';
/*----- Replace links in it ------*/
$my_text = preg_replace('|([\w\d]*)\s?(https?://([\d\w\.-]+\.[\w\.]{2,6})[^\s\]\[\<\>]*/?)|i', '$1 $3', $my_text);
/*----- Echo result ------*/
echo $my_text;
This is the code Im getting the error on:
$show_nav = preg_replace('#\{\$SUBMENU([0-9]+)\}#',implode("\n",$sub_menu['submenu$1']),$show_nav);
So basically I want to replace a string within the $show_nav variable such as {$SUBMENU2} with data from the sub menu array. I tested and it works just fine if I manually put in the number like so:
$show_nav = preg_replace('#\{\$SUBMENU([0-9]+)\}#',implode("\n",$sub_menu['submenu2']),$show_nav);
I also verified the regex is grabbing the proper variable by doing this:
$show_nav = preg_replace('#\{\$SUBMENU([0-9]+)\}#','$1',$show_nav);
It replaces the string with what is found in the {$SUBMENU} string. So if its {$SUBMENU3} it gives me back 3, {$SUBMENU5} it gives me back 5. But I cant seem to get it to dynamically read the $1 variable. I tried adding curly brackets, still same error:
$show_nav = preg_replace('#\{\$SUBMENU([0-9]+)\}#',implode("\n",$sub_menu['submenu{$1}']),$show_nav);
or:
$show_nav = preg_replace('#\{\$SUBMENU([0-9]+)\}#',implode("\n",$sub_menu['{submenu$1}']),$show_nav);
I know Im entering it wrong, but cant figure out the proper way of doing it. Any suggestions?
****UPDATE****
Thanks for the suggestions provided by Toto and Wiktor Stribiżew this is the code that resolved my issue, thanks again!!!
$show_nav = preg_replace_callback(
'#\{\$SUBMENU([0-9]+)\}#',
function($m) use($sub_menu) {
if(isset($sub_menu['submenu' .$m[1]]))
{
return '<ul class="nav-dropdown">' .implode("\n",$sub_menu['submenu' .$m[1]]) .'</ul>';
}
},$show_nav);
preg_replace_callback is your friend:
$show_nav = preg_replace_callback(
'#\{\$SUBMENU([0-9]+)\}#',
function($m) use($sub_menu) {
return implode("\n",$sub_menu['submenu'.$m[$1]])
},
$show_nav);
I know a bit about Regular Expression but really want to learn more about it and now i'm trying to make a function that detects all {} in my content (from a database) and checks what between the brackets. If there is a POST or GET with a name (format: POST:name or GET:name} i would like to replace them with that value.
Example:
When i have a form with the following inputs:
Name
Email
Message
And then in the value attribute i type: {POST:Name}
Then the script must detect the {POST:Name} and will replace it with the string in $_POST['name']. I already searched on Google, but found too much that i don't know what to really use.
Now i have:
<?php
preg_match_all("/{(POST|GET):[.*](})/", $content, $matches, PREG_SET_ORDER);
foreach($matches AS $match)
{
if(isset($_POST[$match]))
$content = str_replace('{POST:'.$match, $_POST[$match], $content);
else
$content = str_replace('{GET:'.$match, $_GET[$match], $content);
}
?>
But this don't work.
You should use preg_replace, better than str_replace.
And if you use preg_replace, you don't need no more your first condition, et can do the same code with only one instruction.
http://fr2.php.net/preg_replace
<?php preg_replace('#{(POST|GET):(.*)}#','$_$1[$2]',$content); ?>
My regex can be false, but something like this should work.
I try replace characters from string and put others , for this use preg_replace_callback function , the problem is this function change order of original text and show differents results
For example if the string it´s :
Hello everybody [1-2-3] this it´s one test [4-5-6-7-8] --- > ORIGINAL TEXT
The script search the [ ] and separate this content with [ ] from the other text , but show me as i put here :
1-2-3 4-5-6-7-8 Hello Everybody this it´s one test --- > BAD RESULT
When the right order it´s the first without [ ]
Hello everybody 1-2-3 this it´s one test 4-5-6-7-8
The script i create :
<?php
$text = " This is a test [gal~ruta~100~100] This is other test [gal~ruta2~100~100]";
function gal($matches)
{
global $text;
$exp=explode("~",$matches[1]);
$end=str_replace($matches[1],$a,$text);
if ($exp[0]=="gal")
{
$a="".$exp[1]."".$exp[2]."".$exp[3]."";
echo $a;
}
}
echo preg_replace_callback(
"/\[(.*?)\]/",
"gal",
$text);
?>
Thank´s everybody for the help
You need to return something from your callback, which will be substituted in the original string. You most certainly don't want to echo, which will send the values straight to the output, in order of execution.
However, it sounds like you don't even need a callback function to remove the brackets around your numbers.
$str = preg_replace("/\[(.*?)\]/", "$1", $str);
CodePad.
I have latex + html code somewhere in the following form:
...some text1.... \[latex-code1\]....some text2....\[latex-code2\]....etc
Firstly I want to obtain the latex codes in an array codes[] to be able to send them to a server for rendering, so that
code[0]=latex-code1, code[1]=latex-code2, etc
Secondly, I want to modify this text so that it looks like:
...some text1.... <img src="root/1.png">....some text2....<img src="root/2.png">....etc
i.e, the i-th latex code fragment is replaced by the link to the i-th rendered image.
I have been trying to do this with preg_replace_callback and preg_match_all but being new to PHP haven't been able to make it work. Please advise.
If you're looking for codez:
$html = '...some text1.... \[latex-code1\]....some text2....\[latex-code2\]....etc';
$codes = array();
$count = 0;
$replace = function($matches) use (&$codes, &$count) {
list(, $codes[]) = $matches;
return sprintf('<img src="root/%d.png">', ++$count);
};
$changed = preg_replace_callback('~\\\\\\[(.+?)\\\\\\]~', $replace, $html);
echo "Original: $html\n";
echo "Changed : $changed\n\nLatex Codes: ", print_r($codes, 1), "Count: ", $count;
I don't know at which part you've got the problems, if it's the regex pattern, you use characters inside your markers that needs heavy escaping: For PHP and PCRE, that's why there are so many slashes.
Another tricky part is the callback function because it needs to collect the codes as well as having a counter. It's done in the example with an anonymous function that has variable aliases / references in it's use clause. This makes the variables $codes and $count available inside the callback.