Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: PREC Modifers

  1. #1
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default PREC Modifers

    So I've been using the modifers (http://php.net/manual/en/reference.p....modifiers.php) I thought correctly but have run into a few problems recently.

    I had this

    Code:
    $code_is = preg_replace('/<li><p.*?>(.*?)<\/p>.*?<\/li>/s', "<li>$1</li>", $code_is);
    I then tried

    Code:
    $code_is = preg_replace('/<li><p.*?>(.*?)<\/p>.*?<\/li>/m', "<li>$1</li>", $code_is);
    and

    Code:
    $code_is = preg_replace('/<li><p.*?>(.*?)<\/p>.*?<\/li>/sm', "<li>$1</li>", $code_is);
    None of which worked as I intended. I'm trying to replace any line item with a paragraph in it there are new lines and tabs in there. None of the above solutions work, the below solution somewhat works but not for all occurances so I figure there must be a better way or modifer.

    Code:
    $code_is = preg_replace('/<li>[.|\n|\r|\t]*?<p.*?>(.*?)<\/p>[.|\n|\r|\t]*?<\/li>/s', "<li>$1</li>", $code_is);
    Corrections to my coding/thoughts welcome.

  2. #2
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    sometimes newlines are expressed as \r\n as well. When you say that it is not working I am assuming that you mean it is not matching. I have not worked with the multiline modifier much, but it applies to the use of ^ and $ commands, which you are not using, so I wouldn't worry about it with your situation.

    /s tells the dot to recognize newlines, so I would keep using it in your situation. Using the dot in [.|\n|\r|\t] makes the \n|\r|\t parts meaningless since the dot will match anything for one character. You may as well replace [.|\n|\r|\t] with ".".

    Not promising anything, but could you post an example of what you want matched and what it should look like after it has been processed?

    Just a typo, but your title says PREC when it should be PCRE

    later
    Last edited by james438; 12-18-2010 at 12:17 AM. Reason: grammar
    To choose the lesser of two evils is still to choose evil. My personal site

  3. The Following User Says Thank You to james438 For This Useful Post:

    bluewalrus (12-17-2010)

  4. #3
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Yes, it's not matching I think because of the new lines/tabs I'd like it to just completely ignore any white spaces (tabs, returns, enters, spaces, [&nbsp;, &#160;(if possible) ]).

    Code:
    <li>
    		<p>blah blah blah random text
    </p></li>
    Replaced as

    Code:
    <li>blah blah blah random text</li>
    Corrections to my coding/thoughts welcome.

  5. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    You could pre-process the text by removing all of those with a simple str_replace(). Then you wouldn't need to worry about it. I'm not sure if that would apply to your project though.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    How would I do it with str_replace? That examples is 3 lines of a larger string. I'd want to keep tabs, new lines, etc. if they aren't inside an li.

    I guess a better sample would be:

    Code:
    	<p>Some stuff up here</p>
    
    		<ol>
    			<li>1</li>
    			<li>2</li>
    			<li>
    					<p>blah blah blah random text
    			</p></li>
    		</ol>
    
    <img src="a.jpg" alt="a" />
    
    	<a href="link.html">link text</a>
    The formatting of this would be

    Code:
    	<p>Some stuff up here</p>
    
    		<ol>
    			<li>1</li>
    			<li>2</li>
    			<li>blah blah blah random text</li>
    		</ol>
    
    <img src="a.jpg" alt="a" />
    
    	<a href="link.html">link text</a>
    Corrections to my coding/thoughts welcome.

  7. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Won't work well then. My idea was more for a full HTML (or XML) parser: first strip all white space (since it is irrelevant) then process the tags. But if you are going to need the formatted source code, there's no way around that.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  8. #7
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Why not just use this?

    PHP Code:
    <?php
    $code_is
    ="    <p>Some stuff up here</p>

            <ol>
                <li>1</li>
                <li>2</li>
                <li>
                        <p>blah blah blah random text
                </p></li>
            </ol>

    <img src=\"a.jpg\" alt=\"a\" />

        <a href=\"link.html\">link text</a>"
    ;
    $code_is=preg_replace('/<li>\s{1,}(<p>|\b)/s','<li>',$code_is);
    $code_is=preg_replace('/(\s)+(<\/p>)?<\/li>/s',"</li>",$code_is);
    ?>
    <textarea cols="100" rows="50"><?php print htmlentities($code_is);?></textarea>
    "+" means one or more, which is the same as "{1,}".
    "?" means zero or one, which is another way of saying that it may be present at this location and it might not.
    To choose the lesser of two evils is still to choose evil. My personal site

  9. #8
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Actually, this is better:

    Code:
    $code_is=preg_replace('/<li>\s+(<p>|\b)/s','<li>',$code_is);
    $code_is=preg_replace('/\s*?<\/p>?<\/li>/s',"</li>",$code_is);
    EDIT: even better:

    Code:
    $code_is=preg_replace('/<li>\s+(<p>|\b)/s','<li>',$code_is);
    $code_is=preg_replace('/\s*?<\/p>?\s*?<\/li>/s','</li>',$code_is);
    Last edited by james438; 12-18-2010 at 12:30 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  10. #9
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    If this answers your question, please mark this thread as resolved.
    To choose the lesser of two evils is still to choose evil. My personal site

  11. #10
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    Been busy haven't had a chance to try it quite yet. What does the \b do?
    Corrections to my coding/thoughts welcome.

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
  •