This question already has answers here:
Parse through a string php and replace substrings
(2 answers)
Closed 9 years ago.
OK, that's what I need :
Get all entries formatted like %%something%%, as given by the regex /%%([A-Za-z0-9\-]+)%%/i
Replace all instances with values from a table, given the index something.
E.g.
Replace %%something%% with $mytable['something'], etc.
If it was a regular replacement, I would definitely go for preg_replace, or even create an array of possible replacements... But what if I want to make it a bit more flexible...
Ideally, I'd want something like preg_replace($regex, $mytable["$1"], $str);, but obviously it doesn't look ok...
How should I go about this?
Code:
<?php
$myTable = array(
'one' => '1!',
'two' => '2!',
);
$str = '%%one%% %%two%% %%three%%';
$str = preg_replace_callback(
'#%%(.*?)%%#',
function ($matches) use ($myTable) {
if (isset($myTable[$matches[1]]))
return $myTable[$matches[1]];
else
return $matches[0];
},
$str
);
echo $str;
Result:
1! 2! %%three%%
if you don't want to tell upper from lower,
<?php
$myTable = array(
'onE' => '1!',
'Two' => '2!',
);
$str = '%%oNe%% %%twO%% %%three%%';
$str = preg_replace_callback(
'#%%(.*?)%%#',
function ($matches) use ($myTable) {
$flipped = array_flip($myTable);
foreach ($flipped as $v => $k) {
if (!strcasecmp($k, $matches[1]))
return $v;
}
return $matches[1];
},
$str
);
echo $str;
Related
This question already has answers here:
php 5 strpos() difference between returning 0 and false?
(5 answers)
Closed 4 years ago.
I need to find one of the three words even if written at the beginning of the string and not just in the middle or at the end.
This is my code:
<?php
$string = "one test";
$words = array( 'one', 'two', 'three' );
foreach ( $words as $word ) {
if ( stripos ( $string, $word) ) {
echo 'found<br>';
} else {
echo 'not found<br>';
}
}
?>
If $string is "one test" the search fails;
if $string is "test one" the search is good.
Thank you!
stripos can return a value which looks like false, but is not, i.e. 0. In your second case, the word "one" matches "one test" at position 0 so stripos returns 0, but in your if test that is treated as false. Change your if test to
if ( stripos ( $string, $word) !== false ) {
and your code should work fine.
This question already has an answer here:
Replace templates in strings by array values
(1 answer)
Closed 6 years ago.
$str = "Hello {{name}} welcome to {{company_name}}";
$array = ['name' => 'max', 'company_name' => 'Stack Exchange'];
how to replace {{name}} and {{company_name}} using array value. any php function that return output like
Hello max welcome to Stack Exchange
First create a new array with your search fields, wrapped in curly braces:
$searchArray = array_map(function($value) {
return "{{" . $value . "}}";
}, array_keys($array));
Now you have an array with the tokens you are searching for, and you already have an array of values you want to replace the tokens with.
So just do a simple str_replace:
echo str_replace($searchArray, $array, $str);
Working example: https://3v4l.org/8tPZt
Here's a tidy little function put together:
function fillTemplate($templateString, array $searchReplaceArray) {
return str_replace(
array_map(function($value) { return "{{" . $value . "}}"; }, array_keys($searchReplaceArray)),
$searchReplaceArray,
$templateString
);
}
Now you can call it like this:
echo fillTemplate(
"Hello {{name}} welcome to {{company_name}}",
['name' => 'max', 'company_name' => 'Stack Exchange']
);
// Hello max welcome to Stack Exchange
Example: https://3v4l.org/rh0KX
Checkout this nice function of PHP: http://php.net/manual/de/function.strtr.php
You can find there examples and explanation of it.
$str = "Hello {{name}} welcome to {{company_name}}";
$array = ['{{name}}' => 'max', '{{company_name}}' => 'Stack Exchange'];
print strtr ($str, $array);
I have string like
$string = "string_key::%foo%:%bar%";
, and array of params
$params = array("foo" => 1, "bar" => 2);
How can I replace this params in $string pattern?
Expected result is
string_key::1:2
First, you need to rewrite the $params array:
$string = "string_key::%foo%:%bar%";
$params = array("foo" => 1, "bar" => 2);
foreach($params as $key => $value) {
$search[] = "%" . $key . "%";
$replace[] = $value;
}
After that, you can simply pass the arrays to str_replace():
$output = str_replace($search, $replace, $string);
View output on Codepad
On a personal note, I did this one:
$string = "string_key::%foo%:%bar%";
$params = array("%foo%" => 1, "%bar%" => 2);
$output = strtr($string, $params);
You do not have to do anything else because if there is some value in the array or the string is not replaced and overlooked.
Fast and simple method for pattern replacement.
I'm not sure what will be the fastest solution for you (it depends on the string size and the number of replacement values you will use).
I normally use this kind of function to perform parameterized replacements. It uses preg_replace_callback and a closure to perform the replacement for each percent enclosed word.
function replaceVariables($str, $vars)
{
// the expression '/%([a-z]+)%/i' could be used as well
// (might be better in some cases)
return preg_replace_callback('/%([^%]+)%/', function($m) use ($vars) {
// $m[1] contains the word inside the percent signs
return isset($vars[$m[1]]) ? $vars[$m[1]] : '';
}, $str);
}
echo replaceVariables("string_key::%foo%:%bar%", array(
"foo" => 1,
"bar" => 2
));
// string_key::1:2
Update
It's different from using str_replace() in cases whereby a percent enclosed value is found without a corresponding replacement.
This line determines the behaviour:
return isset($vars[$m[1]]) ? $vars[$m[1]] : '';
It will replace '%baz%' with an empty string if it's not part of the $vars. But this:
return isset($vars[$m[1]]) ? $vars[$m[1]] : $m[0];
Will leave '%baz%' in your final string.
I want to extract numbers from a string in PHP like following :
if the string = 'make1to6' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted
i will be using these returned values for some calculations.' i would like to extract the numeric character before and after the 'to' substring in the entire string. i.e. 1 and 6 are to be extracted
The length of the string is not fixed and can be a max of 10 characters in length.The number can be of max two digits on either side of 'to' in the string.
Some example string values :
sure1to3
ic3to9ltd
anna1to6
joy1to4val
make6to12
ext12to36
thinking of something like :
function beforeTo(string) {
return numeric_value_before_'to'_in_the_string;
}
function afterTo(string) {
return numeric_value_after_'to'_in_the_string;
}
i will be using these returned values for some calculations.
You could use preg_match_all to achive this:
function getNumbersFromString($str) {
$matches = array();
preg_match_all('/([0-9]+)/', $str, $matches);
return $matches;
}
$matches = getNumbersFromString('hej 12jippi77');
Use preg_match with a regex that will extract the numbers for you. Something like this should do the trick for you:
$matches = null;
$returnValue = preg_match('/([\d+])to([\d+])/uis', 'ic3to9ltd', $matches);
After this $matches will look like:
array (
0 => '3to9',
1 => '3',
2 => '9',
);
You should read somewhat on regular expressions, it's not hard to do stuff like this if you know how they work. Will make your life easier. ;-)
You can use a regular expression as such, it should match exactly your specification:
$string = 'make6to12';
preg_match('{^.*?(?P<before>\d{1,2})to(?P<after>\d{1,2})}m', $string, $match);
echo $match['before'].', '.$match['after']; // 6, 12
You can use this:
// $str holds the string in question
if (preg_match('/(\d+)to(\d+)/', $str, $matches)) {
$number1 = $matches[1];
$number2 = $matches[2];
}
You can use regular expressions.
$string = 'make1to6';
if (preg_match('/(\d{1,10})to(\d{1,10})/', $string, $matches)) {
$number1 = (int) $matches[1];
$number2 = (int) $matches[2];
} else {
// Not found...
}
<?php
$data = <<<EOF
sure1to3
ic3to9ltd
anna1to6
joy1to4val
make6to12
ext12to36
EOF;
preg_match_all('#(\d+)to(\d+)#s', $data, $matches);
header('Content-Type: text/plain');
//print_r($matches);
foreach($matches as $match)
{
echo sprintf("%d, %d\n", $match[1], $match[2]);
}
?>
This is what Regular Expressions are for - you can match multiple instances of very specific patterns and have them returned to you in an array. It's pretty awesome, truth be told :)
Take a look here for how to use the built in regular expression methods in php : LINK
And here is a fantastic tool for testing regular expressions: LINK
<?php
list($before, $after) = explode('to', 'sure1to3');
$before_to = extract_ints($before);
$after_to = extract_ints($after);
function extract_ints($string) {
$ints = array();
$len = strlen($string);
for($i=0; $i < $len; $i++) {
$char = $string{$i};
if(is_numeric($char)) {
$ints[] = intval($char);
}
}
return $ints;
}
?>
A regex seems really unnecessary here since all you are doing is checking is_numeric() against a bunch of characters.
$string = 'test check one two test3';
$result = mb_eregi_replace ( 'test|test2|test3' , '<$1>' ,$string ,'i');
echo $result;
This should deliver: <test> check one two <test3>
Is it possible to get, that test and test3 was found, without using another match function ?
You can use preg_replace_callback instead:
$string = 'test check one two test3';
$matches = array();
$result = preg_replace_callback('/test|test2|test3/i' , function($match) use ($matches) {
$matches[] = $match;
return '<'.$match[0].'>';
}, $string);
echo $result;
Here preg_replace_callback will call the passed callback function for each match of the pattern (note that its syntax differs from POSIX). In this case the callback function is an anonymous function that adds the match to the $matches array and returns the substitution string that the matches are to be replaced by.
Another approach would be to use preg_split to split the string at the matched delimiters while also capturing the delimiters:
$parts = preg_split('/test|test2|test3/i', $string, null, PREG_SPLIT_DELIM_CAPTURE);
The result is an array of alternating non-matching and matching parts.
As far as I know, eregi is deprecated.
You could do something like this:
<?php
$str = 'test check one two test3';
$to_match = array("test", "test2", "test3");
$rep = array();
foreach($to_match as $val){
$rep[$val] = "<$val>";
}
echo strtr($str, $rep);
?>
This too allows you to easily add more strings to replace.
Hi following function used to found the any word from string
<?php
function searchword($string, $words)
{
$matchFound = count($words);// use tha no of word you want to search
$tempMatch = 0;
foreach ( $words as $word )
{
preg_match('/'.$word.'/',$string,$matches);
//print_r($matches);
if(!empty($matches))
{
$tempMatch++;
}
}
if($tempMatch==$matchFound)
{
return "found";
}
else
{
return "notFound";
}
}
$string = "test check one two test3";
/*** an array of words to highlight ***/
$words = array('test', 'test3');
$string = searchword($string, $words);
echo $string;
?>
If your string is utf-8, you could use preg_replace instead
$string = 'test check one two test3';
$result = preg_replace('/(test3)|(test2)|(test)/ui' , '<$1>' ,$string);
echo $result;
Oviously with this kind of data to match the result will be suboptimal
<test> check one two <test>3
You'll need a longer approach than a direct search and replace with regular expressions (surely if your patterns are prefixes of other patterns)
To begin with, the code you want to enhance does not seem to comply with its initial purpose (not at least in my computer). You can try something like this:
$string = 'test check one two test3';
$result = mb_eregi_replace('(test|test2|test3)', '<\1>', $string);
echo $result;
I've removed the i flag (which of course makes little sense here). Still, you'd still need to make the expression greedy.
As for the original question, here's a little proof of concept:
function replace($match){
$GLOBALS['matches'][] = $match;
return "<$match>";
}
$string = 'test check one two test3';
$matches = array();
$result = mb_eregi_replace('(test|test2|test3)', 'replace(\'\1\')', $string, 'e');
var_dump($result, $matches);
Please note this code is horrible and potentially insecure. I'd honestly go with the preg_replace_callback() solution proposed by Gumbo.