0

I am trying to find the word and add a number next to it. How could he do? I tried with the code below, but I could not. Could anyone help me? Thank you!

$string = 'I220ABCD I220ABCDEF I220ABCDEFG'
if (preg_match("/I220.*/", $string, $matches)) {
    echo $matches[0];
}

Expected result: I220ABCD9 I220ABCDEF10 I220ABCDEFG11

2
  • Wouldn't explode() be enough for this little job ? Commented Apr 18, 2013 at 22:17
  • what is the criteria of adding a number? Just add any random number? Commented Apr 18, 2013 at 22:17

5 Answers 5

1

Use preg_replace_callback instead like this:

$str = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
$repl= preg_replace_callback('~(I220[^\s]+)~', function($m) {
         static $i=9;
         return $m[1] . $i++;
       }, $str);

echo $repl\n"; // I220AB9 FRRRR CD I221ABCDEF I220AB10 DSFDSF CDEFG
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks! But as I pass one by one?
if my string was so, how would I do to get a code I220? $string = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
My desired output: I220AB FRRRR CD and I220AB DSFDSF CDEFG
You question starts with: I am trying to find the word and add a number next to it. And in your desired output I don't see any number being added.
0

I dont know what your requirnments for adding the number at the end are so i just incremeneted during the loop;

$string = 'I220ABCD I220ABCDEF I220ABCDEFG';

$arrayStrings = explode(" ", $string);
$int = 9;
$newString = '';

foreach($arrayStrings as $stringItem)
{   
    if (preg_match("/I220.*/", $stringItem, $matches)) 
    {

        $stringItem = $stringItem.$int;
        $newString = $newString.$stringItem." ";
        $int++;
    }
}

echo $newString;

3 Comments

Thank you, I could adapt to my system perfectly!
You very welcome, dont forget to mark a question as answered by selecting the checkmark next to the applicable post so future users can benefit from your question
if my string was so, how would I do to get a code I220? $string = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
0

Use preg_replace_callback():

$string = 'I220ABCD I220ABCDEF I220ABCDEFG';
// This requires PHP5.3+ since it's using an anonymous function
$result = preg_replace_callback('/I220[^\s]*/', function($match){
    return($match[0].rand(0,10000)); // Add a random number between 0-10000
}, $string);

echo $result; // I220ABCD3863 I220ABCDEF5640 I220ABCDEFG989

Online demo.

8 Comments

I'm answering your question with a solution.
if my string was so, how would I do to get a code I220? $string = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
@user1997072 What's the desired output ?
I220AB FRRRR CD and I220AB DSFDSF CDEFG
@user1997072 that's the input, I'm talking about the output, you want to add a number right ?
|
0

You'll need to use a catch block in your regex e.g. "/I220([^ ]+)/" and if you want them all, you'll need to use preg_match_all, too.

2 Comments

Thank you! But as I pass one by one?
You can just put it in a loop: foreach($matches as $match) { print_r($match); }
0

preg_replace_callback with your needs:

    $string = 'I220ABCD I220ABCDEF I220ABCDEFG';


   class MyClass{

       private static $i = 9;

       private static function callback($matches){
           return $matches[0] . self::$i++;
       }

       public static function replaceString($string){
            return preg_replace_callback('/I220[^\s]+/',"self::callback",$string);  
       }
   }


   echo(MyClass::replaceString($string));

of course you can edit to class to initialize the way you want

2 Comments

if my string was so, how would I do to get a code I220? $string = 'I220AB FRRRR CD I221ABCDEF I220AB DSFDSF CDEFG';
My desired output: I220AB FRRRR CD and I220AB DSFDSF CDEFG

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.