SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard Zaggs's Avatar
    Join Date
    Feb 2005
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_replace_callback

    Hi Guys!

    I am using the following code to remove <br /> tags from a string of user entered text. Basically the code below should remove any <br /> tags added within <ol></ol> tags (if that makes sense).

    Unfortunately, it doesn't work correctly and I get the following error:

    Warning: htmlentities() expects parameter 1 to be string, array given in /home/user/public_html/classes/main.class.php on line 1212
    Any ideas?

    PHP Code:
    $result nl2br($result);
    $result preg_replace_callback('/<ol>(.*)<\/ol>/is', array('self''RestoreOl'), $result);

    function 
    RestoreOl($text)
        {
            
    $text str_replace(array("<br />\r\n""<br />\r""<br />\n"), "\n"$text);
            return 
    '<ol>' htmlentities($text) . '</ol>';
        } 

  2. #2
    SitePoint Addict dotancohen's Avatar
    Join Date
    Aug 2005
    Location
    Haifa
    Posts
    314
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try replacing the second argument of str_replace with this:
    Code:
    array("\n", "\n", "n")
    This will be your final code:
    Code:
    $result = nl2br($result);
    $result = preg_replace_callback('/<ol>(.*)<\/ol>/is', array('self', 'RestoreOl'), $result);
    
    function RestoreOl($text)
        {
            $text = str_replace(array("<br />\r\n", "<br />\r", "<br />\n"), array("\n", "\n", "n"), $text);
            return '<ol>' . htmlentities($text) . '</ol>';
        }
    . Have a question?
    . . What Is Firefox?
    . . . . What Is Open Office?
    . . . . . . What Is What?

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,197
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    The error isn't been cause because of the str_replace function but because a regular expression returns an array and NOT a string, in your callback function add

    PHP Code:
    echo '<pre>';
    print_r($text);
    echo 
    '</pre>'
    which will output the array allowing you to see which index you need to target within 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
  •