Canman2005 Posted August 1, 2009 Share Posted August 1, 2009 Hi all I have a chunk of text which looks like $mytext = "Title One<br>This is text one<br><br>Title Two<br>This is text two<br><br>Title Three<br>This is text three"; This outputs Title One This is text one Title Two This is text three Title Three This is text three Is it possible to get it to format like this <a href=?id=1>Title One</a> <a href=?id=2>Title Two</a> <a href=?id=3>Title Three</a> Thanks very much Dave Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/ Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 Sorry I forgot to say, the output is like this <h1>Title One</h1><br>This is text one<br><br><h1>Title Two</h1><br>This is text two<br><br><h1>Title Three</h1><br>This is text three So there are <h1> tags between the headings Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888270 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 Is it possible to get it to format like this Of course. I'd explode by the breakline and loop through every third element of the resulting array. Thiw will give us the elements with the titles in. We can use strip_tags to remove the <h1> tags. Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888272 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 Do you have a snippet of code like that? been trying to solve this for last few hours now and no go thanks Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888281 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 Do you have a snippet of code like that? been trying to solve this for last few hours now and no go thanks Why don't you post up what you've tried then and we can see where you're going wrong. Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888283 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 I don't really have anything, i've been looking on forums and trying different bits of code and altering them, but nothing I have found seems to get me on the right track. I am quite new at php but im sure its not impossible to do this will continue my search Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888293 Share on other sites More sharing options...
.josh Posted August 1, 2009 Share Posted August 1, 2009 do { $mytext = preg_replace('~(<h1>)(?!<a)(.*?)(</h1>)~i',"$1<a href='?id=".++$id."'>$2</a>$3",$mytext,1,$replaced); } while ($replaced > 0); Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888312 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 You do love regular expressions don't you CV? Though I thought they wanted to remove the heading tags. <?php $mytext = "<h1>Title One</h1><br>This is text one<br><br><h1>Title Two</h1><br>This is text two<br><br><h1>Title Three</h1><br>This is text three"; $titles = explode("<br>",$mytext); for($x=0;$x<count($titles);$x+=3){ echo "<a href=somepage.php?id=" . (($x/3)+1) . ">" . strip_tags($titles[$x]) . "</a><br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888313 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 incase formatting is a mess, is it possible to just pickup the headings based on a <h1> and </h1> tag I tried using something like $search = '~<h1>(.*)</h1>~s'; preg_match($search, $mytext, $match); echo $match[1]; Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888450 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 I seem to be able to use $search = '~<h1>(.*)</h1>~s';preg_match($search, $mytext, $match);echo $match[1]; but the problem is that is grabs the first and last <h1> tag, whereas I guess I need to return each <h1> tag seperately and then run the code above on each line returned. any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888509 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 Did you try the code i supplied? Unless i miss-interpreted your original description, i think it fits the bill Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888519 Share on other sites More sharing options...
.josh Posted August 1, 2009 Share Posted August 1, 2009 use (.*?) instead of (.*) but you will then find yourself having another problem with your preg_replace in that preg_replace well go through and replace all of them at once in one go. That's why I have a loop, using the additional arguments to limit preg_replace to 1 replace at a time. You also wanted to have the query string id increment so that's thrown in there too. My posted code is also based off the h1 tags. You never clarified whether you wanted to keep the 'h1' tags and linkify them, or replace the h1 tags with the 'a' tags. If you are wanting to keep them and linkify them, I'm pretty sure my previously posted code does what you want. Did you try it? Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888520 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 GingerRobot, I did try your code and it worked fine, the only problem I could see was bad formatting, so for example, it works great with <h1>Title One</h1><br>This is text one<br><br> but if an extra line break is added, such as <h1>Title One</h1><br>This is text one<br><br><br> then it breaks the code and just displays the first result. can you help at all? thanks very much everyone so far Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888524 Share on other sites More sharing options...
GingerRobot Posted August 1, 2009 Share Posted August 1, 2009 Well if the break line tags always come at the end but are variable in number, you could modify my code by using preg_split instead of explode(): $mytext = "<h1>Title One</h1><br>This is text one<br><br><br><h1>Title Two</h1><br>This is text two<br><br><h1>Title Three</h1><br>This is text three"; $titles = preg_split("|(<br>)+|i",$mytext); for($x=0;$x<count($titles);$x+=2){ echo "<a href=somepage.php?id=" . (($x/2)+1) . ">" . strip_tags($titles[$x]) . "</a><br>\n"; } But it may be that regular expression solution would be neater. Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888535 Share on other sites More sharing options...
Canman2005 Posted August 1, 2009 Author Share Posted August 1, 2009 I guess that's why I was wondering if it's more simple to just grab each section of text which is between the tags <h1> and </h1> Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888536 Share on other sites More sharing options...
GingerRobot Posted August 2, 2009 Share Posted August 2, 2009 I guess that's why I was wondering if it's more simple to just grab each section of text which is between the tags <h1> and </h1> Yeah, it probably is: <?php $mytext = "<h1>Title One</h1><br>This is text one<br><br><br><h1>Title Two</h1><br>This is text two<br><br><h1>Title Three</h1><br>This is text three"; preg_match_all('|<h1>(.*?)</h1>|s',$mytext,$matches); foreach($matches[1] as $key => $title){ echo "<a href=somepage.php?id=" . ($key+1) . ">" . $title . "</a><br>\n"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/168393-splitting-formatting-text/#findComment-888784 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.