Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by myyis
at 2024-07-11 23:17:20
Point:500 Replies:8 POST_ID:828959USER_ID:11860
Topic:
PHP Scripting Language;;
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
--------------
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
Expert: duncanb7 replied at 2024-07-13 17:22:55
Thanks for your points
Have a nice day
Duncan
Have a nice day
Duncan
Author: myyis replied at 2024-07-13 17:18:26
Thank you!
Accepted Solution
Expert: duncanb7 replied at 2024-07-12 04:44:56
300 points EXCELLENT
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
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."";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;}}}?> 1:2:3:4:5:6:7:8:9:
Assisted Solution
Expert: Ray Paseur replied at 2024-07-12 04:28:33
200 points EXCELLENT
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
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.phperror_reporting(E_ALL);// SEE http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28474465.html// TEST DATA FROM THE POST AT EE$txt = <<<EODThis is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla blaEOD;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 PRODUCTecho '<pre>';var_dump($txt, $new); 1:2:3:4:5:6:7:8:9:10:11:12:13:14:15:16:17:18:19:20:21:22:23:24:25:26:27:28:29:30:31:32:33:34:35:36:37:38:39:40:41:42:43:44:45:46:47:48:49:50:51:52:53:54:
Expert: duncanb7 replied at 2024-07-12 02:56:13
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
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."";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;}}}?> 1:2:3:4:5:6:7:8:9:
Author: myyis replied at 2024-07-12 00:46:04
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
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
Expert: duncanb7 replied at 2024-07-12 00:02:08
I also fit your example name string requirement as
$name=array(1=>"peter",2=>"john",45=>"jane",67=>"james");
$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."";$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."";?> 1:2:3:4:5:6:7:8:9:10:11:12:13:
Print input string and output string
-----------------------------------------------
-----------------------------------------------
InputString: This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla blaOutputString: This is text bla bla...... name=john bla blala name=james bla bla.....name=jane bla bla 1:2:
Expert: duncanb7 replied at 2024-07-11 23:51:09
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
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."";$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."";?> 1:2:3:4:5:6:7:8:9:10:11:12:13:
Print input string and output string
------------------------------------------------
------------------------------------------------
InputString: This is text bla bla...... name=2 bla blala name=67 bla bla.....name=45 bla blaOutputString: This is text bla bla...... name=Peter bla blala name=Chris bla bla.....name=David bla bla 1:2: