SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: preg help

  1. #1
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    somewhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg help

    I've been learning how to input data into a database through a form and then having that data print out on a page. What I didn't know is that the data may not appear as how I inputed it, like a ' shows as a \ and periods don't show, etc. Well I search around and came across preg_replace. I've been messing around with that and trying others preg lines, but I just can't seem to get all the text to appear as I typed it in. Also I can't get my text to display in paragraphs if I do type them in paragraphs. Can anyone tell me a preg_replace line that can take care of this?

  2. #2
    SitePoint Evangelist Rodney H.'s Avatar
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this might work, using string replace function

    PHP Code:
    <?php
    // select string data
    $string 'Your string from database here....';

    // create array of symbols for line breaks and returns
    $returns = array("\n""\r""\r\n");

    // use string replace to get rid of symbols in favor of paragraph html markup
    $string str_replace($returns,"</p>\n<p>",$string);

    // display string
    echo '<p>' $string '</p>' "\n";

    ?>

    Quote Originally Posted by DKN
    What I didn't know is that the data may not appear as how I inputed it, like a ' shows as a \
    Oh yeah, for this you are talking about magic quotes. Here, read the article.

  3. #3
    SitePoint Addict
    Join Date
    Jun 2005
    Posts
    294
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to always make sure that your quotes are not interfering with what is being passed....Sometimes escaping your quote with \ will cause the Database to inject something you didn't want
    PHP Code:
    echo '<input="textfield" name="first_name">'
    Say you typed in John Doe. Well in this circumstance John Doe WOULD actually be displayed when you passed it through your script

    BUT..
    PHP Code:
    echo "<input="textfield" name="first_name">"
    Would either give you an error anyway or it would pass only JOHN and truncate your data from everything PRIOR to the SPACE. To fix this..one would have to edit that to....
    PHP Code:
    echo "<input=\"textfield\" name=\"first_name\">"
    This would pass the correct John Doe...

    In this next example...
    PHP Code:
    echo '<input=\"textfield\" name=\"first_name\">'
    Would pass something like \John Doe\ that is, if it didn't give you an error. Hope that helps a bit.

    The way I do it is always surround my echos with '.' that way I don't have to \\ ALL the time only in select areas. That and when I want to include a variable all I have to type is '.$variable.' Makes for easier coding...A lot of people create their forms in DreamWeaver and copy and paste. So surrounding in "." would make you go through and add a \ before every " whereas surrounding it with '.' would allow you to skip that for the most part.

  4. #4
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    //for breaks on linefeeds
    $string=preg_replace("/\\r*\\n/","<br />",$string);

  5. #5
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    somewhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm having a difficult time at this. It appears that I can get the slashes to go away, but I can not break my text into paragraphs. Some people have been helping me and it seems they don't know a solution either.

    I've came up with:
    PHP Code:
    $descriptionpreg_replace('/[^ws^.^,]/''',$row['description']); 
    $descriptionnl2br($row['description']); 
    $description=stripslashes($row['description']); 
    It does everything except the paragraphs.

  6. #6
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why do you have so many carets in your regexp? You should have one to represent the beginning (and this isn't even fundamental). Try the nl2br line first, commenting out the others, and see what happens. At least then you know that it's working.

  7. #7
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DKN
    What I didn't know is that the data may not appear as how I inputed it, like a ' shows as a \ and periods don't show, etc.
    What? You probably have magic quotes turned on. Use stripslashes to remove them.

  8. #8
    SitePoint Enthusiast
    Join Date
    Aug 2004
    Location
    somewhere
    Posts
    74
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My magic quotes isn't on and I already took care of the slashes. My problem is creating breaks. Also my nl2br line works when I comment out the others.

  9. #9
    SitePoint Evangelist Scheisskopf's Avatar
    Join Date
    Nov 2004
    Location
    Southampton, UK
    Posts
    537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This time comment out the preg_replace line, and leave the other two. What happens?


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
  •