Small php function requested

Discussion in 'PHP' started by bilalownsu, Apr 1, 2009.

  1. #1
    I am new to php and am trying to write a program. I need help with converting strings. Could some please help me write a function to turn this type of code:

    <span class="navigation_end"><a href="/5-computers">Computers</a> > <a href="/6-bluetooth-adaptor">Bluetooth Adapter</a> > </span>

    in to this:
    Computers > Bluetooth Adapter

    I have the pusado/java code for it but i need someone to translate it to php

    publie String toText(String html){
    boolean a = false; boolean b = false;
    string toRet;
    while (html.length() != 0){
    if (!a) {
    if(next two chars of html != "<a")
    delete them;
    else
    a = ture;
    }
    else
    {
    if(next two chars of html = "</")
    add ">" to toRet;
    a = true;
    else
    add char to toRet;
    }
    return toRet;
    }
     
    bilalownsu, Apr 1, 2009 IP
  2. javaongsan

    javaongsan Well-Known Member

    Messages:
    1,054
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    128
    #2
    just do
    echo '<span class="navigation_end"><a href="/5-computers">Computers</a> > <a href="/6-bluetooth-adaptor">Bluetooth Adapter</a> > </span>'
    Code (markup):
     
    javaongsan, Apr 1, 2009 IP
  3. bilalownsu

    bilalownsu Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I dont want to print it out, I need the refined text for another step in the program.
     
    bilalownsu, Apr 1, 2009 IP
  4. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #4
    function string_to_text($string){
    preg_match_all("/\<a(.*)\>(.*)\<\/a\>/iU", $string, $matches, PREG_SET_ORDER);
    foreach($matches as $match){
    $res .= "{$match[2]} &gt; ";
    }
    return substr($res,0,strlen($res)-6);
    }
    
    $new_var = string_to_text($links);
    PHP:
     
    emed, Apr 1, 2009 IP
  5. bilalownsu

    bilalownsu Peon

    Messages:
    122
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried to test the code and it gives an error. I think there is something wrong with in the pattern but i cant figure it out.

    http://codepad.org/57KqFEtN
     
    bilalownsu, Apr 1, 2009 IP
  6. emed

    emed Peon

    Messages:
    70
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #6
    i found this: http://drupal.org/node/17271 its the same error, i dont know if thats your case

    it work fine on my computer
    PHP Version 5.2.6
    
    PCRE (Perl Compatible Regular Expressions) Support 	enabled
    PCRE Library Version 	7.6 2008-01-28 
    Code (markup):
    i think your php dont have pcre support, and i dont know what to do in that case sorry
     
    emed, Apr 2, 2009 IP