String manipulation

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

String manipulation

Post by kayeb »

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,
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

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;
?>
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

Post by kayeb »

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,
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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);
}
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

Post by kayeb »

Thank you Weirdan.
It is much appreciated.

(And I'm pretty sure I understand it too, hehehe!)
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

Post by kayeb »

Although I cannot get it to work. I noticed a typo but still can't get it working ... any ideas?

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); 
}
Any ideas? Where did $val come from?
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

Post by kayeb »

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)

Thanks,
User avatar
Derfel Cadarn
Forum Contributor
Posts: 193
Joined: Thu Jul 17, 2003 12:02 pm
Location: Berlin, Germany

Post by Derfel Cadarn »

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)
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!

Good luck.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Oops :oops:. Replace PREG_MATCH_ORDER with PREG_PATTERN_ORDER.

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 ("{([^}]+)}")
kayeb
Forum Newbie
Posts: 6
Joined: Thu Dec 18, 2003 12:37 pm

Post by kayeb »

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 :D

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.
User avatar
bradvan
Forum Newbie
Posts: 10
Joined: Mon Jun 02, 2003 8:26 pm
Location: Rylstone, Australia
Contact:

Post by bradvan »

You are missing the mysql result resource in mysql_fetch_row()
code should read:

Code: Select all

$result = mysql_query("select color,id from colors where color in(".implode(",",$matches&#1111;1]).")");
  while(list($col,$id)=mysql_fetch_row($result))
      $myString = str_replace('&#123;'.$col.'&#125;',$id,$myString);
Post Reply