Guest Thunderhawk66 Posted April 29, 2006 Share Posted April 29, 2006 I have a script that uses a Regular Expression to get data from a textarea.For example:[code]preg_match_all("/Address:.[^\\n]*/i", $str, $matches);foreach ($matches as $val) {echo $val[1];}[/code]but I was wondering if there is a way to print a line of text two lines above it?Eg if there is this text:HelloBlahAddress: 123and the code prints "Address: 123", how would I get it to print the text two lines above it ("Hello")?Thanks,-Thunderhawk66. Quote Link to comment Share on other sites More sharing options...
Twentyoneth Posted April 29, 2006 Share Posted April 29, 2006 [code]echo "Hello<br>Blah<br>" . $val[1];[/code] Quote Link to comment Share on other sites More sharing options...
Guest Thunderhawk66 Posted April 29, 2006 Share Posted April 29, 2006 Thanks for that, but I meant if a user entered text in the textarea, and I found some text called "Address:", could I print the second line that was above it (if I didn't know what was entered).That "Hello, Blah" was an example of what could be entered in the textarea. Quote Link to comment Share on other sites More sharing options...
Orio Posted April 29, 2006 Share Posted April 29, 2006 Use the nl2br function, then use the explode function (split by "<br />").Then find the part with the "Address: " (lets say it was line in "n"), and echo $var[n] and $var[n-2].Orio. Quote Link to comment Share on other sites More sharing options...
Guest Thunderhawk66 Posted April 29, 2006 Share Posted April 29, 2006 That sounds like it should work, thanks.But could you maybe give an example?I don't understand what you mean by "lets say it was line in "n"".Here's some of the code I have:[code]$str = nl2br(rtrim($_POST[text]));$num = 0;$i = 0;while ($i < 200){preg_match_all("/Address:.[^\\n]*/i", $str, $matches);foreach ($matches as $val) {$txtol = $val[$num];$expl = explode("<br />", $txtol);echo $expl[$num-2];}}$i++;$num++;[/code]Is that the way I do it?Thanks for your help. Quote Link to comment Share on other sites More sharing options...
Guest Thunderhawk66 Posted May 1, 2006 Share Posted May 1, 2006 Can someone help?Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.