Using preg_replace to get $_GET variables - php

I made the following PHP function:
<?php
function convertGET($str) {
$regex = '/GET:+([a-zA-Z0-9_]+)/';
$str = preg_replace($regex, $_GET["$1"], $str);
return($str);
}
$string = "foobar: GET:foobar";
$string = convertGET($string);
echo $string;
?>
The function is suppost to get a string and replace something like:
GET:foobarwith the $_GET variable "foobar".

Use preg_replace_callback() instead:
<?php
$input = array("foobar" => "Some other string");
$regex = '~GET:([a-zA-Z0-9_]+)~';
$string = "foobar: GET:foobar";
$string = preg_replace_callback($regex,
function($matches) use ($input) {
return $input[$matches[1]];
},
$string);
echo $string;
// output: foobar: Some other string
?>
See a demo on ideone.com.

Related

preg_replace_callback() walk around

Would like to do something like this:
$str = '<a>lalala</a>';
echo preg_replace('~<a>(.*)</a>~i','<a>'.str_replace('la','mi',"$1").'</a>',$str);
So it would return:
<a>mimimi</a>
But can't use preg_replace_callback() function. Any simple ideas?
You could use preg_replace_callback() to do specific operations in replacement:
$str = '<a>lalala</a>';
echo preg_replace_callback('~<a>(.*)</a>~i',function($matches){
return '<a>'.str_replace('la','mi',$matches[1]).'</a>';
},$str);
Outputs:
<a>mimimi</a>
Or with preg_match():
$str = '<a>lalala</a>';
if (preg_match('~<a>(.*)</a>~i', $str, $matches)) {
$str = '<a>'.str_replace('la','mi',$matches[1]).'</a>';
};
echo $str;

Trying to call a function inside preg_replace

$string = 'Hello [user=1]';
$bbc = array('/\[user=(.*?)\]/is');
$replace = array(user('textlink',$1));
$s = preg_replace($bbc , $replace, $string);
echo $s
How do I change $1 in preg_replace with a function?
If I understand you correctly, you want to execute user() function on each match? As mentioned in comments, use the preg_replace_callback() function.
<?php
$string = 'Hello [user=1]';
$s = preg_replace_callback(
'/\[user=(.*?)\]/is',
function($m) {return user('textlink', $m[1]);},
$string
);
echo $s;
You may use a preg_replace_callback_array:
$string = 'Hello [user=1]';
$bbc = array(
'/\[user=(.*?)\]/is' => function ($m) {
return user('textlink',$m[1]);
});
$s = preg_replace_callback_array($bbc, $string);
echo $s;
See PHP demo.
You may add up the patterns and callbacks to $bbc.

Replace content between two tags

I want to replace all content between ( and ), using php.
my string:
$string = "This is a (string)";
the required output is:
$string = "This is a";
my code doesn't works:
$string = "This is a (string)";
$search = "/[^(](.*)[^)]/";
$string = preg_replace($search, "", $string);
echo $string; // output is ")"
$result = preg_replace('/\(.+?\)/sm', 'Put your text here', $string);
Try this code
Or to save () add them to replacement
$page = preg_replace('/\(.+?\)/sm', '(Put your text here)', $page);
This should work for you:
<?php
$string = "This is a (string)";
echo preg_replace("/\([^)]+\)/","",$string);
?>
Output:
This is a
Just change that:
$search = "/ *\(.*?\)/";

How to create a function that can transform the last word into Uppercases?

How do i create a function replaceMe() in php that would turn:
$str = 'This is a very long string';
into:
'This is a very long STRING?'
can someone help me?
You apparently want to do a regular expression substitution, anchored at the end of the line. Use preg_replace:
$str = 'This is a very long string';
# This is a very long LINE
echo preg_replace("/string$/", "LINE", $str);
For a general case, you can provide a callback instead of a replacement string, and simply uppercase the matched substring with preg_replace_callback:
$str = 'This is a very long blah';
function word_to_upper($match) {
return strtoupper($match[1]);
}
# This is a very long BLAH
echo preg_replace_callback("/(\w+)$/", "word_to_upper", $str);
If you're using PHP 5.4 or greater, you can supply the callback as an anonymous function:
echo preg_replace_callback("/(\w+)$/", function ($match) {
return strtoupper($match[1])
}, $str);
This works:
$str = 'This is a very long string';
echo $str."<br/>";
function replaceMe($str = "")
{
$words = explode(" ",$str);
$totalwords = count($words)-1;
$lastword = $words[$totalwords];
$words[$totalwords] = strtoupper($lastword);
$str = implode(" ",$words);
return $str;
}
echo replaceMe($str);
?>
Output:
This is a very long string
This is a very long STRING
$str = 'This is a very long string.';
function lastWordUpper($str){
$temp = strrev($str);
$last = explode(" ", $temp, 2);
return strrev(strtoupper($last[0]). ' ' .$last[1]) ;
}
echo lastWordUpper($str);

String Replace In PHP

i have a long string like
$str = "this is [my] test [string] and [string] is very long [with] so many pwords]"
i know str_replace function, but when i replace
$str = str_replace( "[", "<a href=\"/story.php?urdu=",$str;
$str = str_replace( "]", "\"></a>",$str;
i got this result
but i want this result for each word in []
test
You should use preg_replace() for this one.
Code
<?php
function replace_matches($matches) {
$text = htmlspecialchars($matches[1]);
$url = urlencode($matches[1]);
return "{$text}";
}
$str = "this is [my] test [string] and [<script>alert(1)</script>] is very long [with] so many pwords]";
$str = preg_replace_callback("%\[(.+?)\]%", "replace_matches", $str);
echo $str;
?>
Working Example
$str = preg_replace_callback(
'/\[([^\]]*)\]/',
function($matches) {
return sprintf('%s',
urlencode($matches[1]),
htmlspecialchars($matches[1]));
},
$str);

Categories