Replace all links with url index?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Jonson

    #1

    Replace all links with url index?

    Hello,

    I'm writing a newsletter poster for a website. I have some
    html news items which I want to turn into plain text and prettify
    before emailing it.

    Now, i have found wordwrap and strip_tags so the only problem
    remaining is the links. Below you see what I like to see, but
    I haven't got a clue how to get there.

    $html = '<p>My <a href="http://go.there/stuff">link somewhere</a>.</p>';
    doSomeMagic($ht ml);
    echo $html;

    After doSomeMagic I'd like the echo to look like this:

    My link somewhere[1].
    [1] http://go.there/stuff

    That is, in the text I want an reference, and below the entire paragraph
    i want a link index.

    I'd be grateful for any hints how to get there.
    --
    Fredrik Jonson
  • Jon Kraft

    #2
    Re: Replace all links with url index?

    Fredrik Jonson <pt97fjo@studen t.bth.se> wrote:
    [color=blue]
    > I'm writing a newsletter poster for a website. I have some
    > html news items which I want to turn into plain text and prettify
    > before emailing it.
    >
    > Now, i have found wordwrap and strip_tags so the only problem
    > remaining is the links. Below you see what I like to see, but
    > I haven't got a clue how to get there.
    >
    > $html = '<p>My <a href="http://go.there/stuff">link somewhere</a>.[/color]
    </p>';[color=blue]
    > doSomeMagic($ht ml);
    > echo $html;[/color]

    Hi Fredrik,

    I wrote something a while ago that does that:

    <?php
    $lcount=0;
    $globMatches;

    function increase(){
    global $lcount;
    $lcount++;
    return $lcount;
    }

    function stripLinks($ori gtext){
    global $globMatches;
    $filteredtext = "";
    $pattern = "/(<a href=\")(.+?)(\ ")(\s*)(>)(.+?) (<\/a>)/e";
    $replace = "'\\6['.increase().']'";
    preg_match_all( $pattern, $origtext, $matches);
    $globMatches[] = $matches;
    $filteredtext = preg_replace($p attern, $replace, $origtext);
    return $filteredtext;
    }

    function makeLinkFooter( ){
    global $globMatches, $lcount;
    $appendix = "";
    $m=1;
    for($j=0;$j<$lc ount;$j++){
    for($i=0;$i<cou nt($globMatches[$j][2]);$i++){
    $appendix .= "[".$m."] ".$globMatc hes[$j][2][$i]."<br>\n";
    $m++;
    }
    }
    return $appendix;
    }

    $html = 'My <a href="http://go.there/stuff">link somewhere</a>.';
    $html_new = stripLinks($htm l);
    $footer = makeLinkFooter( );

    echo $html_new."\n\n ";
    echo $footer."\n";
    ?>

    Hope this helps;
    JOn

    Comment

    • Fredrik Jonson

      #3
      Re: Replace all links with url index?

      In <Xns93B26DC4A8D 81jonjonuxcouk@ 130.133.1.4> Jon Kraft wrote:
      [color=blue]
      > I wrote something a while ago that does that:
      > [snip - some code]
      > Hope this helps; JOn[/color]

      Yes, thanks. I had already started working with some code using
      preg_replace_ca llback, which I of course stumbled on _right_ after
      I posted my first message. But I had problems with the regex, which
      with some hints from your code I got to work.

      <?php
      $current_link = 0;
      $link_index;

      function link_indexer($m atches)
      {
      global $link_index;
      global $current_link;
      // save the match in the global index
      $link_index[$current_link] = $matches[1];
      $current_link++ ;
      // return the "plain" text with a reference.
      return $matches[2] . '[' . $current_link . ']';
      }

      $html = '<p>My <a href="http://go.there/stuff">link somewhere</a>.</p>';

      $text = preg_replace_ca llback(
      '/<A href=\"(.+?)\"> (.+?)<\/A>/i',
      'link_indexer',
      $html);
      // remove other html tags
      strip_tags($tex t);

      // print the plain version
      echo $text; // renders: "My link somewhere[0]."

      // print the index
      $i = 0;
      foreach ($link_index as $link) {
      print "[$i] $link\n";
      $i++;
      }
      // renders: "[0] http://go.there/stuff"
      ?>

      --
      Fredrik Jonson

      Comment

      Working...