SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Just a simple QU on echoing out html. If I've got a load of HTML that I want to echo what is the most simple way of getting round the problem of having charactors within the html that need to be backslashed, without having to go through all the HTML and backslashing every individual element?

    Say I've got this html.

    <a herf ="http:www.mysite.com"><b> Link<b></a>

    Can I use:
    echo addslashes("<a herf ="http:www.mysite.com"><b> Link<b></a>"); ?????

    Along the same lines, if I want to include a html page within part of a script and the echo it out do I also need to use addslashes to the include function to get it to work without errors???





  2. #2
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you could use this:


    printf("<a href="http:www.mysite.com"><b>Link<b></a>");


    I'm not positive, but I think the printf function has no problem with quotes.

    If you want to use variables, I believe (correct me if I'm wrong Freddy ) they work like this:


    printf("<a href="http:www.mysite.com"><b>%s<b></a>, $linktitle");


    Here's the official entry on PHP.net: http://www.php.net/manual/en/function.printf.php

    Hope that helps.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting, I'd like to know more about the in's and out's of the printf and echo functions with respect to this - if it is the case, particularly with regard to what to do when 'including' html files.

    Hopefully Freddie will be able to throw some light!

  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)
    If you are not including varaibles in a string you should enclose it with ' not ", e.g.

    echo '<a href="http:www.mysite.com"><b> Link<b></a>';

    Would print what you are after, if you want to include variables then you have two choices e.g.

    1) echo "<a href=\"$url\"><b> Link<b></a>";

    2) echo '<a href="'.$url.'"><b> Link<b></a>';

    The first method is OK for short strings or long strings with lots of variables included but for long strings with 0, 1 or 2 variables included you should use the second method.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  5. #5
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers,

    The: echo '<a href="http:www.mysite.com"><b> Link<b></a>';

    would seem a sensible way of includeing an echoing a html page with no variables.

  6. #6
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To follow up on that you can use printf('');

    the same way Karl suggested

    printf('<a href="%s"><b> Link<b></a>', $url);

    Notice the single quotes around everything

    TWTCommish's example of

    printf("<a href="http:www.mysite.com"><b>%s<b></a>", $linktitle);

    Would return a parse error.

    The syntax is the same for echo or printf() its just printf allows you to specify vars at the end instead of concatentating them into the middle of the echo statement
    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
  •