Using Str_Replace So That It Only Acts on the First Match

Using str_replace so that it only acts on the first match?

Can be done with preg_replace:

function str_replace_first($search, $replace, $subject)
{
$search = '/'.preg_quote($search, '/').'/';
return preg_replace($search, $replace, $subject, 1);
}

echo str_replace_first('abc', '123', 'abcdef abcdef abcdef');
// outputs '123def abcdef abcdef'

The magic is in the optional fourth parameter [Limit]. From the documentation:

[Limit] - The maximum possible
replacements for each pattern in each
subject string. Defaults to -1 (no
limit).


Though, see zombat's answer for a more efficient method (roughly, 3-4x faster).

str_replace match only first instance

You need to use preg_replace() instead:

$_POST['exclude'] = preg_replace( '/' . preg_quote( $match[1], '/' ) . '/', "", $_POST['exclude'], 1, $i );

The variable after the $_POST['exclude'] is a limit variable, as you can see in the link above.

The preg_quote() function wasn't necessary in the date field, but because its a variable, it may be needed to include the special regular expression characters.

php replace first occurrence of string from 0th position

Use preg_replace() with a limit of 1:

preg_replace('/nothing/', 'something', $str, 1);

Replace the regular expression /nothing/ with whatever string you want to search for. Since regular expressions are always evaluated left-to-right, this will always match the first instance.

Replace only first match using preg_replace

The optional fourth parameter of preg_replace is limit:

preg_replace($search, $replace, $subject, 1);

Using preg_replace to replace only first match

preg_replace()'s does a Regular Expression match and replace. You are passing it a string instead of a valid RegEx as the first argument.

Instead you might be looking for str_replace() which does string replacement.

str_replace replace only match words

Update:

HD, thank you! your solution works for me!

This is work version

function replace_text_wps($text){

$dir = plugin_dir_path( __FILE__ );
$file= $dir."bad2.list";

$badlist = file($file, FILE_IGNORE_NEW_LINES);

$replacement = "[CENSORED]";
$badlist = array_map(function($v) { return "\b". $v ."\b"; }, $badlist);
foreach($badlist as $f) {
$text = preg_replace("/".$f."/u", $replacement, $text);


return $text;
}

str_replace is replacing only the first variable in an array in php

You are moving the result of the str_replace() into a new variable inside the loop and therefore losing changes.

$array_content[]=array("USER_NAME", "John");  
$array_content[]=array("LOGIN_LINK", 'http://exampl.com/signin');
$array_content[]=array("SITE_NAME", 'Google');

$content = "Welcome to {{SITE_NAME}}, Hi, {{USER_NAME}}, now you can login here: {{LOGIN_LINK}}";

foreach ($array_content as $key=>$value){
$content = str_replace("{{" . $value[0] . "}}", $value[1],$content);
//^^^ changed here and ^^ ^^
}
$mailcontent = stripslashes($content);

You could also do this instead as str_replace() accepts arrays for param 1 and 2

$array_content = array("{{USER_NAME}}"=> "John", 
"{{LOGIN_LINK}}"=> 'http://exampl.com/signin',
"{{SITE_NAME}}"=> 'Google');

$find = array_keys($array_content);
$replace = array_values($array_content);

$content = "Welcome to {{SITE_NAME}}, Hi, {{USER_NAME}}, now you can login here: {{LOGIN_LINK}}";

$mailcontent = str_replace($find, $replace,$content);

echo $mailcontent;

replace only the first instance of a substring

You should use preg_replace with $limit param

preg_replace('/BIN/', 'SYSTEM', $patha, 1);

Because str_replace $count param is not limit as you waiting for



Related Topics



Leave a reply



Submit