Jump to content


This topic is now archived and is closed to further replies.


Getting a needle from the haystack

Recommended Posts

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)
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)
(other text here)
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
[code]$mytext=get_that_string($the_long_string, "/^- SERIES (.*.) - STAFF -/"); [/code]
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!

Share this post

Link to post
Share on other sites
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...
[code]// 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) ) {
   // determinates the array chunk where "recording" begins.
   if (strpos($row_serie, $in_string)) {
   // 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.. ;)

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.