String manipulation
Moderator: General Moderators
String manipulation
Hi,
I am a complete newbie and was wondering if anyone could point me in the right direction please.
I have a string ($myString) which consists of -
'Some of the colors present are {red}, {yellow} and {green}, but {black} or {sky blue} are nowhere to be seen'
I also have a table with these colors in which have id numbers associated with them, like-
Table colors:
id color
1 red
2 blue
3 yellow
4 purple
5 sky blue
etc
What I would like to do is replace the colors in the string (and their surrounding braces) with the id numbers, so the string would appear as: 'Some of the colors present are 1, 3 and 6, but 7 or 5 are nowhere to be seen'.
Could someone please advise me of the best way to go about this please.
I assume that I would first have to strip out all of the parts which are inside the braces and put them into an array. Then do a select on the db using LIKE, for each instance of the array to get the id number, and then use an eregi_replace to replace the occurances with their id numbers? Hehehe, you can tell I'm a complete newbie, huh? There's probably a much easier way.
Any help would be very much appreciated.
Thanks,
I am a complete newbie and was wondering if anyone could point me in the right direction please.
I have a string ($myString) which consists of -
'Some of the colors present are {red}, {yellow} and {green}, but {black} or {sky blue} are nowhere to be seen'
I also have a table with these colors in which have id numbers associated with them, like-
Table colors:
id color
1 red
2 blue
3 yellow
4 purple
5 sky blue
etc
What I would like to do is replace the colors in the string (and their surrounding braces) with the id numbers, so the string would appear as: 'Some of the colors present are 1, 3 and 6, but 7 or 5 are nowhere to be seen'.
Could someone please advise me of the best way to go about this please.
I assume that I would first have to strip out all of the parts which are inside the braces and put them into an array. Then do a select on the db using LIKE, for each instance of the array to get the id number, and then use an eregi_replace to replace the occurances with their id numbers? Hehehe, you can tell I'm a complete newbie, huh? There's probably a much easier way.
Any help would be very much appreciated.
Thanks,
Perhaps this will give you more ideas?
Code: Select all
<?php
// a string as your own
$string = 'Some of the colors present are {red}, {yellow} and {purple}, but {black} or {a noodle} are nowhere to be seen';
// array (change to your database one if wanted)
$colarr = array(
'red',
'blue',
'yellow',
'purple',
'sky blue'
);
// start the loop
for ($i=0;$i<count($colarr);$i++) {
$string = str_replace('{'.$colarr[$i].'}',$i,$string).'<br>';
}
// echo the finished string
echo $string;
?>Yes that helps.
Thank you very much JAM.
Hmmm, one thing though. Say my table of had 10,000,000 rows in it, would it still be practical to do it that way? i.e. retrieve all of the rows and place them in an array and then compare the relevant string parts for replacement? Or am I not looking at it the correct way? (total newbie, remember)
Thanks,
Thank you very much JAM.
Hmmm, one thing though. Say my table of had 10,000,000 rows in it, would it still be practical to do it that way? i.e. retrieve all of the rows and place them in an array and then compare the relevant string parts for replacement? Or am I not looking at it the correct way? (total newbie, remember)
Thanks,
no, it's not practical if you have a lot of records... may be this will be better
Code: Select all
$myString='Some of the colors present are {red}, {yellow} and {purple}, but {black} or {a noodle} are nowhere to be seen';
preg_match_all("/{([^}]+)}/",$myString,$matches,PREG_MATCH_ORDER);
if(count($matches[1])){
foreach($matches[1] as $id=>$val)
$matches[1][$id]="'".$val."'";
mysql_query("select color,id from colors where color in(".implode(",",matches[1]).")");
while(list($col,$id)=mysql_fetch_row())
$myString = str_replace('{'.$col.'}',$id,$myString);
}Although I cannot get it to work. I noticed a typo but still can't get it working ... any ideas?
Any ideas? Where did $val come from?
Code: Select all
$myString='Some of the colors present are {red}, {yellow} and {purple}, but {black} or {a noodle} are nowhere to be seen';
preg_match_all("/{([^}]+)}/",$myString,$matches,PREG_MATCH_ORDER);
if(count($matches[1])){
foreach($matches[1] as $id=>$val)
$matches[1][$id]="'".$val."'";
mysql_query("select color,id from colors where color in(".implode(",",$matches[1]).")");
while(list($col,$id)=mysql_fetch_row())
$myString = str_replace('{'.$col.'}',$id,$myString);
}- Derfel Cadarn
- Forum Contributor
- Posts: 193
- Joined: Thu Jul 17, 2003 12:02 pm
- Location: Berlin, Germany
Yup, you are right: those regex'es are quite difficult. As a tip, when you're wondering about a php-command, always check php.net first! They've got a great reference-site. You can find you're preg_match_all there too!kayeb wrote:Also, could someone please explain the contents of the preg_match_all string pattern, because I don't quite understand it (a bit thick I guess)
Good luck.
Oops
. Replace PREG_MATCH_ORDER with PREG_PATTERN_ORDER.
Ok, I'll try to break up the pattern:
Ok, I'll try to break up the pattern:
- original pattern is /{([^}]+)}/
- forward slashes is just delimiters, so we'll omit them for the sake of clarity hereunder so pattern is {([^}]+)}
- It reads in English as: search for the opening curly brace ("{([^}]+)}") followed by the sequence of any characters except closing brace ("{([^}]+)}") [store that sequence as a subpattern ("{([^}]+)}")] followed by closing curly brace ("{([^}]+)}")
Thanks Weirdan, that helps explain the pattern. I did check PHP.net for the pre_match_all function but still couldn't make head or tail of the string pattern, but now I know 
I still have a minor problem with the code though as I get an error 'Wrong parameter count for mysql_fetch_row()'. I can't figure it out though ... looks fine to me ... although I am very new to this.
Thanks for all you help so far, it is much appreciated.
I still have a minor problem with the code though as I get an error 'Wrong parameter count for mysql_fetch_row()'. I can't figure it out though ... looks fine to me ... although I am very new to this.
Thanks for all you help so far, it is much appreciated.
- bradvan
- Forum Newbie
- Posts: 10
- Joined: Mon Jun 02, 2003 8:26 pm
- Location: Rylstone, Australia
- Contact:
You are missing the mysql result resource in mysql_fetch_row()
code should read:
code should read:
Code: Select all
$result = mysql_query("select color,id from colors where color in(".implode(",",$matchesї1]).")");
while(list($col,$id)=mysql_fetch_row($result))
$myString = str_replace('{'.$col.'}',$id,$myString);