SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Member bola's Avatar
    Join Date
    Apr 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    String replacement question

    How can i create string replacement like this:
    <a href="link"><img src="picture" align="center(left,right)"></a>
    for example like this:
    [L="link"][img align="align"]imagename.jpg[/img][/L]

  2. #2
    killall -9 lusers
    Join Date
    Oct 2002
    Location
    Cincinnati, Ohio, USA
    Posts
    390
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: String replacement question

    Originally posted by bola
    How can i create string replacement like this:
    <a href="link"><img src="picture" align="center(left,right)"></a>
    for example like this:
    [L="link"][img align="align"]imagename.jpg[/img][/L]
    Use PCRE functions

  3. #3
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'd use a regular expression. Something like this
    Code:
    $st = '<a href="link"><img src="picture" align="center(left,right)"></a>';
    
    // first we process <IMG> tag ...
    $st = preg_replace('/<img src="([^"]+)" align="([^"]+)">/i','[img align="\2"]\1[/img]',$st)
    
    // .. then <A>
    $st = preg_replace('/<a href="([^"]+)">([^<]+)<\/a>/i','[L="\1"]\2[/L]',$st)
    
    // note that the order is irrelevant
    Paul

  4. #4
    SitePoint Guru enygmadae's Avatar
    Join Date
    Sep 2002
    Location
    Dallas, Tx.
    Posts
    795
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $string
    ='[L="link"][img align="align"]imagename.jpg[/img][/L]';
    preg_match("/\[L=\"([a-zA-Z0-9\.\-]*)\"\]\[img align=\"(align|center|left|right)\"\]([a-zA-Z0-9\.\-]*)\[\/img\]/",$string,$matches);
    print_r($matches);
    echo 
    "<a href=\"$matches[1]\"><img src=\"$matches[3]\" align=\"$matches[2]\"></a>";
    ?>
    you know, you'll never learn regexps if you don't try first....
    PHP News, Views and Community: http://www.phpdeveloper.org

  5. #5
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by enygmadae
    you know, you'll never learn regexps if you don't try first....
    Yip, that's right. bola you have now two example, have fun with regex If you have any questions you can post it here.

    Paul

  6. #6
    SitePoint Member bola's Avatar
    Join Date
    Apr 2002
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok. I'll have some fun now , and see if I can make it ...


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
  •