Jump to content

Splitting & Formatting Text


Canman2005

Recommended Posts

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.