change repeating phrase in a text

I want to change a specific phrase in text like below:

--------------
This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla
--------------

I  want to  get every integer coming after "name="  and alter them using a  function (the function is not important).

Therefore the result will be:

------------------------
This is text bla bla...... name='John' bla blala name='james' bla bla.....name='jane' bla bla
----------

Note that after the phrase 'name='  always comes an integer and then a blank.  The count of the phrase in the text  may be more  than 3 .

Can anybody help me?

Thank you
LVL 1
myyisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

duncanb7Commented:
Please test the code as follows  for reference only not fully tested
I have assigned the name number to name string in two-dimension array of $name
such as $name=array(1=>"John",2=>"Peter",45=>"David",67=>"Chris");
you can change it for assigning each name you want .

Hope understand your question completely.If not. please point it out
Duncan
<?php
$str="This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla";
echo "InputString:  ".$str."\n";
$mline=explode("name=",$str);$cnt=count($mline);
$name=array(1=>"John",2=>"Peter",45=>"David",67=>"Chris");
for ($i=1;$i<$cnt;$i++){$kline=explode(" ",$mline[$i]);
foreach ($name as $k => $v) {
 if ($k==$kline[0]){$kline[0]=$v;break;}}
$mline[$i]=implode(" ",$kline);
}
$str=implode("name=",$mline);
echo "OutputString: ".$str."\n";
?>

Open in new window

Print input string and output string
------------------------------------------------
InputString:  This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla
OutputString: This is text bla bla...... name=Peter bla blala name=Chris bla bla.....name=David bla bla

Open in new window

0
duncanb7Commented:
I also fit your example  name string requirement as
$name=array(1=>"peter",2=>"john",45=>"jane",67=>"james");
<?php
$str="This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla";
echo "InputString:  ".$str."\n";
$mline=explode("name=",$str);$cnt=count($mline);
$name=array(1=>"peter",2=>"john",45=>"jane",67=>"james");
for ($i=1;$i<$cnt;$i++){$kline=explode(" ",$mline[$i]);
foreach ($name as $k => $v) {
 if ($k==$kline[0]){$kline[0]=$v;break;}}
$mline[$i]=implode(" ",$kline);
}
$str=implode("name=",$mline);
echo "OutputString: ".$str."\n";
?>

Open in new window

Print input string and output string
-----------------------------------------------
InputString:  This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla
OutputString: This is text bla bla...... name=john bla blala name=james bla bla.....name=jane bla bla

Open in new window

0
myyisAuthor Commented:
I don't want to use  the array $name, I want it to be a function something like this, (don't bother the details of the function)

function change_number_to_name ($number) {
$sql=Select name from NAMES where id=$number
return $result('name');
}

How can we  change it for a function
Thank you very much
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

duncanb7Commented:
Is that what you want the function of chg_num_name() as follows
and it will return the name according to the number ?
Be reminded. You tag in this post is "php" not any sql related
<?php
$result =chg_num_name(2);
echo $result."\n";
function chg_num_name($num){
$name=array(1=>"peter",2=>"john",45=>"jane",67=>"james");
foreach ($name as $k => $v) {
 if ($k==$num){return $v;}}
}
?>

Open in new window

0
Ray PaseurCommented:
Do you have a 1:1 relationship between numbers and names, with the numbers being unique values?  If so you can use an array with keys=numbers and values=names.  This is probably a sensible design.  Some care must be given to the order of replacement since you might have a number "2" and a number "12."  Both would be substrings ending in a "2" and followed by a blank.  For this reason a regular expression would be appropriate in the replacement algorithm.

As with most questions here at E-E, the quality of the answer is directly related to the clarity of the question and the quality of the test data you provide for us.  This seems to test out correctly.  If there is more to this question, please post back or post a new question and I'll be glad to help.
http://iconoun.com/demo/temp_myyis.php

<?php // demo/temp_myyis.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28474465.html

// TEST DATA FROM THE POST AT EE
$txt = <<<EOD
This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla bla
EOD;

function number_name
( $str              // THE STRING TO BE MUTATED
, $arr              // THE ARRAY OF NUMBERS AND MATCHING NAMES
, $sig='name='      // THE SIGNAL SUBSTRING
, $ci=NULL          // SET TO 'i' FOR CASE-INSENSITIVE MATCHING OF SIGNAL SUBSTRING
)
{
    // PAD STRING WITH A BLANK
    $str .= ' ';

    // USE EACH ELEMENT OF THE ARRAY
    foreach ($arr as $num => $nom)
    {
        $rgx                // REGEX LOCATOR
        = '#'               // REGEX DELIMITER
        . preg_quote($sig)  // LITERAL STRING SIGNAL
        . $num              // LITERAL STRING DIGITS
        . ' '               // LITERAL STRING BLANK
        . '#'               // REGEX DELIMITER
        . $ci               // REGEX FLAGS
        ;
        $rep                // REGEX REPLACEMENT STRING
        = $sig              // LITERAL STRING
	    . $nom              // THE NAME
	    . ' '               // A BLANK
	    ;
        $str = preg_replace($rgx, $rep, $str);
    }
    return rtrim($str);
}

// USE THE FUNCTION
$nns = array
(  2 => 'Myyis'
,  4 => 'John'
, 67 => 'Venkat'
, 45 => 'Patel'
)
;
$new = number_name($txt, $nns);

// SHOW THE WORK PRODUCT
echo '<pre>';
var_dump($txt, $new);

Open in new window

0
duncanb7Commented:
Now I know why you don't want $name array in my posts, you just want the array from sql database after php operation input into the function of chg_num_name, Right ?If so, I modifiy this.
Hope understand your question completely.If not, please write all input and output you want from
the function.

Duncan
 
<?php
$result =chg_num_name(2,$yoursqlarray);
echo $result."\n";
function chg_num_name($num, $yoursqlarray){
///////////////$name=array(1=>"peter",2=>"john",45=>"jane",67=>"james");
foreach ($yoursqlarray as $k => $v) {
 if ($k==$num){return $v;}}
}
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
myyisAuthor Commented:
Thank you!
0
duncanb7Commented:
Thanks for your points

Have a nice day

Duncan
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.