Jump to content

Recommended Posts

Hi everyone!

 

I wondered whether there's a way of counting the number of replacements made from a string replacement.

 

Example:

$string="I like strings. I think they are brilliant. I will get to the bottom of this issue.";
$word_to_replace="I";
$replacement="We";
string=preg_replace($word_to_replace, $replacement, $string);
echo $string;

 

That would just output:

We like strings. We think they are brilliant. We will get to the bottom of this issue.

 

But what I need (strange as it might seem) is this:

We1 like strings. We2 think they are brilliant. We3 will get to the bottom of this issue.

 

I understand loops and how to increase a counting variable, but not how to do this with a preg_replace function.

 

Any suggestions?

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/182495-counting-replacements-in-strings/
Share on other sites

I am not sure if this is the optimum way to do it but it kinda works

 

<?php
$counter = 1;
$string="I like strings. I think they are brilliant. I will get to the bottom of this issue.";
$string=preg_replace("#I#e", 'W.$counter++' , $string);
echo $string;
?>

actually the code should have been, this will not throw a notice

 

<?php
$counter = 1;
$string="I like strings. I think they are brilliant. I will get to the bottom of this issue.";
$string=preg_replace("#I#e", '"W".$counter++' , $string);
echo $string;
?>

that's straight from phps documentation.

mixed preg_replace  ( mixed $pattern  , mixed $replacement  , mixed $subject  [, int $limit = -1  [, int &$count  ]] )

If specified, this variable will be filled with the number of replacements done.

 

nm I just read your post all the way through.  You'll probably have to use a callback function to increment your $replacement.

 

$string="I like strings. I think they are brilliant. I will get to the bottom of this issue.";
$word_to_replace="I";
$replacement="We";
string=preg_replace($word_to_replace, $replacement, $string);
echo $string;

 

It didn't work because you didn't test your example code, you missed a $ from before "string" and and "I" is not a regular expression...

 

$string="I like strings. I think they are brilliant. I will get to the bottom of this issue.";
$word_to_replace="#I#";
$replacement="We";
$string=preg_replace($word_to_replace, $replacement, $string, -1, $count);
echo '<pre>' . "\r\n";
echo 'Output:' . "\t\t\t" . $string . "\r\n";
echo 'Replacements made:' . "\t" . number_format($count, 0) . "\r\n";
echo '</pre>';

 

Output:			We like strings. We think they are brilliant. We will get to the bottom of this issue.
Replacements made:	3

 

Baaa. I feel sheepish.

 

Eventually I did this as a 2 stage approach like this:

#original (case insensitive) word from database:
$uc_word=ucwords($word);
$upper_word=strtoupper($word);
$pattern = array(
'~\b'.$word.'\b(?![^<]*?>)~',
'~\b'.$uc_word.'\b(?![^<]*?>)~',
'~\b'.$upper_word.'\b(?![^<]*?>)~'
);

#Replacement highlighted phrase:	
$new_word = array(
'<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$word.'</span>',
'<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$uc_word.'</span>',
'<span id="'.$word_all.'_nst_id_007" class="'.$style.'">'.$upper_word.'</span>'
);
#now highlight the words:
$page = preg_replace($pattern, $new_word, $page);

Then:

$replacement='nst_id_';
$counter=1;
$page=preg_replace("#nst_id_007#e", '"$replacement".$counter++', $page);

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.