Jump to content


Photo

Getting a needle from the haystack


  • Please log in to reply
1 reply to this topic

#1 s_bastian

s_bastian
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 May 2006 - 09:17 AM

Hi!
I have a very long string, (say a description of a game divided in tech data, description etc), and I would like to be able to do some str_replace() operation on only one part of it. Example
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]- TECHNICAL DATA -
(some text here)
- SERIES -
1. [!--coloro:red--][span style=\"color:red\"][!--/coloro--]Dynasty Wars[!--colorc--][/span][!--/colorc--] (1989)
2. [!--coloro:red--][span style=\"color:red\"][!--/coloro--]Warriors of Fate[!--colorc--][/span][!--/colorc--] (1992)
- STAFF -
(other text here)
[/quote]
I want to change the red strings with a link. This means I need to trim out od the text the "SERIES" chapter and work on that. And this is my problem: HOW can I do this? is there any kind of regular espression function that puts the matched text into a string? something like
$mytext=get_that_string($the_long_string, "/^- SERIES (.*.) - STAFF -/");
retunrning a $mytext stating
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]- SERIES -
1. Dynasty Wars (1989)
2. Warriors of Fate (1992)
- STAFF -[/quote]
(no problem in replacing the names with the links, I can dig that... ;)

I was planning to do this by splitting the string on ^-, and working then on the array that would result from this, but isn't there any faster way? Searched the function list, but all string check functions I found states if the needle is in the haystack, but do not return the needle....
Many thanks for any reply!

#2 s_bastian

s_bastian
  • New Members
  • Pip
  • Newbie
  • 6 posts

Posted 29 May 2006 - 12:57 PM

ok, after I was confirmed by PM that there is no direct, single function way to do what I need, I solved the thing by myself, developing the paranoid fist idea I had (working with the exploded array with array search and replace) into this piece of code, maybe a little clumsy but effective...
// var setup

$hist="a loto of text"; // this is the main text I need to parse to get my infos
$inserie=0; // determinates if we have to consider the lines or not
$in_string=" SERIE -"; // the word/flag in $hist from where what I'm interested in begins
$out_string="^-"; // the word/flag in $hist from where what I'm interested in ends
$serie=""; // a string the rebuild the "original" text portion
$new_serie=""; // the ouptut string

// the job
// I explode the main text in an array line by line
$work_serie=explode("\n", $hist);
// print_r($work_serie); //debug // I use these debug lines to undesrtand where I get stuck

foreach ($work_serie as $row_serie) {

  $row_serie=trim($row_serie); //cleanin up of data

   // determinates the array chunk where "recording" ends. It precedes the line that starts the recording because in my case, also starting line would match stop, causing no recording at all
   if (preg_match("/$out_string/", $row_serie) ) {
     $inserie=0;
   }
   // determinates the array chunk where "recording" begins.
   if (strpos($row_serie, $in_string)) {
     $inserie=1;
   }
   // the recording of data
   if ($inserie==1) {
       $serie .="$row_serie\r\n"; // This is the cleaned up data, rebuilt to it's original form
       $new_serie.="<b>$row_serie</b><br>"; // This is the worked out data. Final version will do some more str_replace and also some mysql querys
     }
}

// echo $new_serie; //debug
// gets the final ouput
$hist=str_replace($serie, $new_serie, $hist);
My knowledge stops here, if someone knows how to improve it, any advice is welcome (and feel free to use it if you need.. ;)





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users