SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I read the PHP article on how to get rid of HTML altogether with PHP replace commands.

    Can you send me a list of "replace" commands that does all the basic HTML stuff?

    I alread have the following:
    - bold [b]xxx[eb]
    - italic [i]xxx[ei]
    - page break [PAGEBREAK]

    What I need are the following:
    - bullets <ul><li>xxx</li></ul>
    - image display <img src=xxx>
    - enlarge fonts <font size=xx></font>

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let me guess you are working on a bulletin board or a content management system, right?

    Well the best method is to use regular expressions to do the dirty work for you an example:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    $res = eregi_replace( quotemeta( '&lt;u&gt;' ), quotemeta( '[u]' ), $string );
    [/code]
    and to go the other way:
    <BLOCKQUOTE><font size="1" face="Verdana, Arial">code/font><HR><pre>
    $res = eregi_replace( quotemeta( '[u]' ), quotemeta( '&lt;u&gt;' ), $string );
    [/code]
    and of course you can do more complex replacements but that's a mini tutorial on its own.

    Let me clarify something first though, you want to get rid of HTML e.g. &lt;b&gt; and replace it with [b]? Your post doesn't make it completaly clear.

    ------------------
    Karl Austin
    KDA Web Services
    "Everyone has a photographic memory. Some just don't have film."

  3. #3
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. I want to create a completely HTML-free content management program.

    When I have to put on a image, I need to type all those codes including the attributes and stuff. I just want to minimize the time to type all the HTML.

    If I want to display an image, I would type something like this.
    [image=http://www.url.com/xxx.gif]

    If I want to list three items on a bullet list, I would type something like this.
    [l] One [el]
    [l] Two [el]
    [l] Three [el]

    Is this possible?

  4. #4
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You mean like UBB code on these forums?

    If you do then it is possible and I have some regular expressions for doing just that.

    They take your text with your markup, [u] etc. and transform it into HTML.

    The other choice you have is to write your documents in XML and use the Expat libraries in PHP to parse the document (And guess what - I have some scripts for doing that as well, it uses a generic XML parsing script and then reads the application specific stuff from a database.)

    ------------------
    Karl Austin
    KDA Web Services
    "Everyone has a photographic memory. Some just don't have film."

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    usb,

    From your original post, I gather you've read the section of my article series dealing with regular expressions and how to use them to replace custom tags like these with their HTML equivalents.

    Everything you've asked so far is just an application of the techniques I taught there. Granted, the precise examples you're after aren't given, but those that are should be sufficient to teach you everything you need to know.

    Consider your image tag, for example:

    [IMG=http://url.for.the/image.gif]

    The regular expression to match this needs to detect [IMG= followed by a URL, which it needs to remember, followed by ]. Here's the regular expression:

    \[IMG=([-_./a-zA-Z0-9!&%#?,'=:~]+)]

    Breaking this down:

    \[IMG= matches [IMG= (the backslash prevents the [ from being interpreted as the start of a list of characters to match.

    [-_./a-zA-Z0-9!&%#?,'=:~] matches any single letter, number, or symbol listed between the square backets (e.g. a URL).

    [-_./a-zA-Z0-9!&%#?,'=:~]+ matches any string of characters composed of the those listed between the square brackets.

    ([-_./a-zA-Z0-9!&%#?,'=:~]+) is the same as the previous, but remembers the string so that we can use it as \\1 in our replacement string.

    ] matches the closing square bracket following our URL.

    Now, we want to replace matches of this regular expression with the following:

    &lt;img src="\\1"&gt;

    Note that \\1 is the URL stored by the regular expression above. The complete PHP code for replacing [IMG] tags is thus:

    $newtext = ereg_replace( "\[IMG=([-_./a-zA-Z0-9!&%#?,'=:~]+)]", "&lt;img src=\"\\1\"&gt;", $oldtext);

    Make sense? This was by far the most difficult tag of those you asked for. Now see if you can come up with the rest on your own. Let us know about any problems you encounter!


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

  6. #6
    SitePoint Enthusiast
    Join Date
    Dec 1999
    Posts
    85
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You are very helpful in this matter. I was hoping if you can give me some hints on how to replace bullets too...hehe...I am not trying to make you do all the coding but I really don't know how to do it.

    &lt;ul&gt;
    &lt;li&gt;Item one&lt;/li&gt;
    &lt;li&gt;Item two&lt;/li&gt;
    &lt;li&gt;Item three&lt;/li&gt;
    &lt;/ul&gt;

  7. #7
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Why not just have [UL], [LI], [ELI], and [EUL] tags (for example) and replace them with their HTML equivalents just as my article does for [B] and [I]?


    ------------------
    -Kevin Yank.
    http://www.SitePoint.com/
    Helping Small Business Grow Online!

  8. #8
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I did have code here for doing it but UBB trashed it so I deleted it. I posted code for url's, lists and email tags (Better than using a url link to do the same job. If anyone else wants the code then please email me or ICQ me and I will gladly give them it.

    ------------------
    Karl Austin
    KDA Web Services
    "Everyone has a photographic memory. Some just don't have film."

    [This message has been edited by Karl (edited July 31, 2000).]


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
  •