Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts

    Help with strings

    All,
    I have the follwing in a text string:

    <link>construction<endlink>

    I'd like to be able to search this in a string and basically find the word/words inbetween <link> and <endlink> and make it an actual link, so:

    <a href="construction.php">construction</a>

    Also, there could be multiple of these in a string. Any ideas?? I also have the following code:
    PHP Code:
    $txt $resultsettxt['text'];
    $newtxt str_replace("<endbullet><bullet>""</li><li>"$txt);
    $newtxt str_replace("<bullet>""<li>"$txt);
    $newtxt str_replace("<endbullet>""</li><br>"$newtxt);
    $newtxt str_replace("<bold>""<strong>"$newtxt);
    $newtxt str_replace("<endbold>""</strong>"$newtxt);
    echo 
    nl2br($newtxt); 
    Which works good and want to continue to use this so if there is a way to incorporate this with the <link> then I'm open to ideas.

    Thanks in advance!

  • #2
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Also, if there is anyway if it looks like this:

    <link>Construction<endlink>

    To only use the capitalized word in the word part of the link and make it lowercase in the URL part. So this would look like this:

    <a href="construction.php">Construction</a>

    Thanks!

  • #3
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Any ideas on this?? Thanks!

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    take a look at www.php.net/preg_replace

  • #5
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    I did something similar to that with the str_replace but don't know how to get the value(s) between the <link> and <endlink>

  • #6
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Just guessing here, not tested

    PHP Code:
    $newtext preg_replace('/\<link\>(.*?)\<\/endlink\>/is','<a href="'.$1.'.html">'.ucfirst($1).'</a>',$txt); 

  • #7
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    PHP Code:
    $txt preg_replace('/\<link\>(.*?)\<endlink\>/''<a href="\\1.php">\\1</a>'$txt); 
    The "\\1" part refers to the bracketed match; the "(.*?)".

    The match says "zero or more instances of any character" the "?" makes it "ungreedy" so it doesn't break "<link>a<endlink> <link>b<endlink>" by matching "a<endlink> <link>b" instead of "a" and "b" separately.

    This may well be over your head, in which case as soon as you try to make the matching more complex, say in order to support something like "<link title=foo's title>foo<endlink>" you'll run into big problems; I'd advise that you really look into learning about regular expressions in more depth.
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #8
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    So I tried to do what you did with the following code:
    PHP Code:
    $testtxt "<link>Test<endlink>";
    $testtxt preg_replace('/\<link\>(.*?)\<endlink\>/''<a href="\\1.php">\\1</a>'$testtxt);
    echo 
    "The test text is".$testtxt
    This works fine. However when I pull it from the db it doesn't work:
    PHP Code:
    $qrytxt "Select * from texts where text_id='2'";
    $resulttxt mysql_query($qrytxt);
    $resultsettxt mysql_fetch_array($resulttxt);
    $txt $resultsettxt['text'];
    $newtxt preg_replace('/\<link\>(.*?)\<endlink\>/''<a href="\\1.php">\\1</a>'$txt);
    echo 
    $newtxt
    Any ideas why that wouldn't work?

  • #9
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    What is $txt at the time you echo $newtxt?
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini

  • #10
    Regular Coder
    Join Date
    Oct 2009
    Posts
    438
    Thanks
    9
    Thanked 7 Times in 7 Posts
    Ok, I did a little troubleshooting and figured it out. The only other thing I would like to happen is regardless of what is selected in the middle. Like if it's "Thanks", I'd like it to go to "thanks" so always lowercase.

  • #11
    New Coder
    Join Date
    May 2007
    Location
    Manchester, UK
    Posts
    72
    Thanks
    0
    Thanked 2 Times in 2 Posts
    http://php.net/preg_replace_callback
    http://php.net/var_dump
    http://php.net/strtolower

    Depending on your preference you might want to use an anonymous function for the callback: http://php.net/manual/en/functions.anonymous.php
    Last edited by Ahri; 03-11-2010 at 03:07 PM.
    Every PHP programmer of any skill level should set error_reporting(E_ALL); at the top of their scripts or in their php.ini


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •