[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

change repeating phrase in a text

Posted on 2014-07-11
8
Medium Priority
?
152 Views
Last Modified: 2014-07-13
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
0
Comment
Question by:myyis
  • 5
  • 2
8 Comments
 
LVL 13

Expert Comment

by:duncanb7
ID: 40191862
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
 
LVL 13

Expert Comment

by:duncanb7
ID: 40191866
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
 
LVL 1

Author Comment

by:myyis
ID: 40191891
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:duncanb7
ID: 40192030
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
 
LVL 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 800 total points
ID: 40192098
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
 
LVL 13

Accepted Solution

by:
duncanb7 earned 1200 total points
ID: 40192110
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
 
LVL 1

Author Closing Comment

by:myyis
ID: 40193553
Thank you!
0
 
LVL 13

Expert Comment

by:duncanb7
ID: 40193555
Thanks for your points

Have a nice day

Duncan
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This holiday season, we’re giving away the gift of knowledge—tech knowledge, that is. Keep reading to see what hacks, tips, and trends we have wrapped and waiting for you under the tree.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses
Course of the Month18 days, 3 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question