SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Replacing some and keeping some

    Hi,

    I've made my own markup language for the chapters in my database, very similar to the vBulletin markup as used here in the forums. Replacing the markup tags with their HTML equivalent is no problem as long as the markup doesn't have any property (like [P] -> <P> or [B] -> <B>).

    However, when it comes to markup with properties like
    [SOMEURL=http://www.thissite.com]This site[/SOMEURL] I don't know how to replace the URL markup with a A HREF tag but still keep the URL. Can anyone tell me how this is accomplished?

    Thanks very much in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is how I would do it(vb kills the second backslash behind each number 1 and 2 in the replace clause of the ereg_replace() function, so make sure you have two backslashes):

    PHP Code:
    $str "sdfhsfdhgsfgsfdjg jf kfsj sfk sf [SOMEURL=http://www.yahoo.com]This Site[/SOMEURL]";

    /*
    Match anything after SOMEURL= and before ] then match 
    anything after ] and before [/SOMEURL] and replace the
    placeholders with those values.
    */
    $str ereg_replace("\[SOMEURL=(.*)\](.*)\[/SOMEURL\]""<a href=\"\\\1\">\\\2</a>"$str); 
    Last edited by freddydoesphp; Jun 13, 2001 at 05:40.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've tried putting to backslashes in front of each number, but that doesn't work. Have I misunderstood you regarding the placement of the backslashes?

    Thanks in advance !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's see if I can get what I want here, you need two backslashes before the 1 and two backslashes before the 2

    $str = ereg_replace("[SOMEURL=(.*)](.*)[/SOMEURL]", "<a href=\"\\1\">\\2</a>", $str);

    Also if tthat doesn't work could you post the code you tried, because it works for me.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Here's the code I use, copied and pasted (the two backslashes before number 1 and 2 are removed by vB, but they're there):

    PHP Code:
    <?php

    $str 
    "Here is a [SOMEURL=http://www.link.com]link[/SOMEURL].";

    $str ereg_replace("[SOMEURL=(.*)](.*)[/SOMEURL]""<a href=\"\\1\">\\2</a>"$str); 

    echo(
    $str);

    ?>
    You can to to http://www.ureb.no/test.php and see what sort of result it produces.

    Is there something I'm missing?
    Sincerely,

    Eirik Johansen
    Netmaking AS

  6. #6
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Here's all the code you should need, with a full explanation, from my massive article.

    http://www.webmasterbase.com/article.php/228/807
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes vb screwed up another part of it as well but each [ and ] need to be escaped as well

    $str = ereg_replace("\[SOMEURL=(.*)](.*)\[/SOMEURL\]", "<a href=\"\\1\">\\2</a>", $str);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to you both, guys. Now it works like a charm!
    Sincerely,

    Eirik Johansen
    Netmaking AS

  9. #9
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    I've encountered an unexpected problem with the code after all. Here's the function as I use it now.

    PHP Code:
    $tempcontent ereg_replace("\\[SOMEURL=(.*)](.*)\\[/SOMEURL]""<a href=\"\\\\1\">\\\\2</a>"$tempcontent); 
    The problem is that when there exists more than one link in the $tempcontent variable, the function takes the very first opening tag and the very last closing tag and interpreters everything inbetween as one link. Check out the source code at http://www.netmaking.com/articles/index.php/1/2 to see what I mean.

    What syntax should I use so that the function only reads the text until the first closing tag in encounters?

    Thanks in advance !
    Last edited by Kevin Yank; Jun 19, 2001 at 09:58.
    Sincerely,

    Eirik Johansen
    Netmaking AS

  10. #10
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The code in my article (URL given above) does not have this problem, as the regular expression does not allow square brackets to occur between the start and end tags.

    Another option is to use the Perl-Compatible Regular Expressions (PCRE) functions in PHP4 (e.g. preg_replace) and use the 'non-greedy' version of (.*) for the code between your tags: (.*?).
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  11. #11
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Totally off the subject here, but it seems Eirik didn't want to look at your article "Building database..."

    I'd just like to say I thought it was awesome. In fact my very first (and very basic) go with it is here. Use code 100.

    Just thought I'd let you know how great it was!
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is another option, I didn't have a lot of time to take into account for security issues, but this seems to work provided all links will be in the form of [SOMEURL=http://somedomain.com]some domain[/SOMEURL]


    PHP Code:
    $start = array("[""SOMEURL""=""]");
    $end = array("<""a ""href="">");
    $str "Here is a [SOMEURL=http://www.link1.com]li_nk1[/SOMEURL] and I will put another one in to test it [SOMEURL=http://www.link2.com]here[/SOMEURL].";
    $str str_replace($start$end$str); 
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Here is another option, I didn't have a lot of time to take into account for security issues, but this seems to work provided all links will be in the form of [SOMEURL=http://somedomain.com]some domain[/SOMEURL]


    PHP Code:
    $start = array("[""SOMEURL""=""]");
    $end = array("<""a ""href="">");
    $str "Here is a [SOMEURL=http://www.link1.com]li_nk1[/SOMEURL] and I will put another one in to test it [SOMEURL=http://www.link2.com]here[/SOMEURL].";
    $str str_replace($start$end$str); 
    str_replace is a whole lot faster than ereg_replace as well. I wonder how many str_replace operations like that can be done per second vs. the ereg method? Would be useful for high-traffic vB sites which has to run that stupid parsing over and over and over (I recommend running it upon insert and update that way you don't have to touch it again).

  14. #14
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The strength of the above code, its elegant simplicity, is also its downfall. Unfortunately, the above will convert any occurence of '[' to '<', any occurrence of 'SOMEURL' to 'a ', any occurrence of '=' to 'href=', and any occurrence of ']' to '>'.

    If the above global conversions are acceptable to you, then by all means use this code; otherwise, you may want to stick with the regular expressions method.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  15. #15
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Perhaps do something like this:
    Code:
    <url="xyz"> i am lazy </url>
    Then, to increase the laziness factor, simply search for:
    <url=

    and replace with
    <a href=

    Then search for
    </url>

    and replace with
    </a>

    Of course at that point why not just allow HTML? That is what I did.

  16. #16
    E-business guru Eirik's Avatar
    Join Date
    Nov 2000
    Location
    Oslo, Norway
    Posts
    413
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again,

    Totally off the subject here, but it seems Eirik didn't want to look at your article "Building database..."
    Actually, I've read large parts of Kev's comprehensive article several times, and have even printed it for easy reference. I too find it a great resource.

    I've also managed to solve the problem using the example in the article. I first tried adapting the example which would convert URLs to hyperlinks, but then I scrolled down a bit further and noticed that you had included an example of exactly what I was trying to accomplish.

    Thanks to you all, guys !
    Sincerely,

    Eirik Johansen
    Netmaking AS

  17. #17
    We like music. weirdbeardmt's Avatar
    Join Date
    May 2001
    Location
    Channel Islands Girth: Footlong
    Posts
    5,882
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No offence intended, Eirik! Just seemed like you hadn't read it, when I reckon anyone wanting to learn PHP/MySQL stuff MUST read it!
    I swear to drunk I'm not God.
    » Matt's debating is not a crime «
    Hint: Don't buy a stupid dwarf ö Clicky

  18. #18
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    MattR, my code was not written out of laziness, it seemed like a decent alternative to me, since he was having trouble getting the ereg_replace function working. I am a volunteer here, as you are, and so I don't have 12 hours a day to write code for people, when someone needs a question answered I do the best of my ability to give them an answer, and a good one at that.

    Sometimes the answers aren't quite what others feel are up to par, well that is fine I can take it when someone finds a flaw in my code and suggests a better way. But please do not refer to me as being lazy, I am not lazy and my intention with this post was not to be lazy, so I find it just plain wrong when you stroll in here and accuse me of being lazy.

    With that said, my assumption with the str_replace is that the person writing the script will say one of two things. One yes this will work for me because I have filtered all imcoming text and made sure that the chacters used in my str_replace are the only ones that need changing(ie removed all extraneous brackets and equal signs). Now then the code will work fine. TWo, he says no I need to make sure ONLY links get changed so I had better revert back to the ereg_replace.

    So take it however you want, but they are both valid methods for achieving his goal given a set of criteria is met.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  19. #19
    Database Jedi MattR's Avatar
    Join Date
    Jan 2001
    Location
    buried in the database shell (Washington, DC)
    Posts
    1,107
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa man; I wasn't implying that you were lazy. I was speaking of my own laziness (I *hate* regular expressions and avoid them at all costs; hence the 'screw it just allow HTML' comment), also commenting on the laziness of the PHP parsing engine as well.

    When you have a high-trafficed site and this is run 25 times a second (for one page display for instance) it might be a good trade-off between speed and 'bugginess' (e.g. strreplace is case sensitive so you have to search for [URL, [UrL], if you want it to be 'perfect'; otherwise stick to one format, etc.) -- regular expressions are notoriously slow and if I can make the PHP engine work less hard (better than 'lazy' I guess) I'm all for it.
    Last edited by MattR; Jun 20, 2001 at 08:19.

  20. #20
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry if I sounded to defensive. You are absolutely correct though, why not just them put html in there, or even just automatically turn http://www.somedomain.com into a link, instead of making them wrap [SOMEURL] around text they want to be a link.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


Bookmarks

Posting Permissions

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