MadnessRed Posted August 26, 2008 Share Posted August 26, 2008 how would I go about detecting the text between two strings. For example. blah blah blah [countdown]12467946446[/countdown] more blah the script would search for anything between [countdown] and [/countdown] then make the output $countdown that way I could do countdown($countdown,false). I have already made the countdown function and know how to only get numbers from the string. I'm just not sure how to find it. Would you do something like find the position of the [countdown] and [/countdown] in the text and then only take that bit? Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/ Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 Will there be only one pair, or multiples? Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626143 Share on other sites More sharing options...
MadnessRed Posted August 26, 2008 Author Share Posted August 26, 2008 potentially multiples. for example there might be $string = "Woo, [countdown]1230163200[/countdown] until Christmas. That means its [countdown]1230768000[/countdown] until 2009."; And then something like $orig = '#[countdown][^"]*[/countdown]#'; $cd = countdown($1); $rep = $cd['days']." Days, ".$cd['hours']." Hours and ".$cd['minutes']." Minutes. " $string = preg_replace($orig,$rep,$string); Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626146 Share on other sites More sharing options...
MadnessRed Posted August 26, 2008 Author Share Posted August 26, 2008 I have got this far. while ($row = mysql_fetch_array($query2)){ $entry = $row['Entry']; $entry = preg_replace('#[countdown][^"]*[/countdown]#',countdown("\\1",false),$entry); //Line 109 echo "<tr>"; echo "<td style='text-align: left'><h3><u><strong>".$row['Name']."</strong></u></h3></td>"; echo "<td style='text-align: right'><i>".date("jS F Y", $row[Date])."</i></td>"; echo "</tr><tr>"; echo "<td colspan='2' style='text-align: left'>".$entry."<hr /><br /></td>"; echo "</tr>"; } And get this error. Fatal error: Cannot pass parameter 1 by reference in /home/evo/public_html/madnessred/pages/home.php on line 109 Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626163 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 <pre> <?php $string = "Woo, [countdown]1230163200[/countdown] until Christmas. That means its [countdown]1230768000[/countdown] until 2009."; function countdown ($in) { ### Calculations here. return $in; } echo preg_replace('%\[countdown\](.*?)\[/countdown\]%e', 'countdown("$1")', $string); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626239 Share on other sites More sharing options...
MadnessRed Posted August 26, 2008 Author Share Posted August 26, 2008 firstly what does <pre> do? secondly, where the function is, can I just put the include there? <?php $string = $row['Entry']; include('countdown.php'); $entry = preg_replace('%\[countdown\](.*?)\[/countdown\]%e', 'countdown("$1")', $string); ?> Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626309 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 firstly what does <pre> do? I use it in all of my examples because it's oftentimes required for printing arrays and such; see http://www.w3schools.com/TAGS/tag_pre.asp. You can disregard. secondly, where the function is, can I just put the include there? Yes. Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626318 Share on other sites More sharing options...
MadnessRed Posted August 26, 2008 Author Share Posted August 26, 2008 does the function have to be there? I mean if I wanted to add more functions to that file, could I just include it at the top? Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626340 Share on other sites More sharing options...
effigy Posted August 26, 2008 Share Posted August 26, 2008 Be where? I used a function in my example because I didn't have an include. Whatever functions you call have to exist. Link to comment https://forums.phpfreaks.com/topic/121425-get-text-between-2-string/#findComment-626357 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.