php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #16501 preg_replace() seems to act in strange with some escaped chars.
Submitted: 2002-04-08 17:32 UTC Modified: 2002-04-09 10:24 UTC
From: haug at berndhaug dot net Assigned:
Status: Not a bug Package: PCRE related
PHP Version: 4.0.6 OS: SuSE GNU/Linux 7.3
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: haug at berndhaug dot net
New email:
PHP Version: OS:

 

 [2002-04-08 17:32 UTC] haug at berndhaug dot net
Preface:
Was writing a script to replace single linebreaks in input to <br/> and multiple to </p><p class="content"> such as to parse user scribbling to very simple xhtml. Multiplatform linebreaks should be accepted.

There, I think I encountered very strange behaviour of PHP concerning octally escaped chars.

If I did something very very stupid, please don't hate me too much, I'm not too experienced w/ php. I however first showed this to a friend who is and he didn't know what it was either.

Short script:
------------------------
// parsed all newlines as single ones, resulting in a many <br/>s --
// two (!) for a linebreak, 1 for every added linebreak in the input
parseUserTextfield1($input) {
$parsed=preg_replace(array("/(\012\015|\012|\015)/","/(\012\015|\012|\015)(\012\015|\012|\015)+/"),
	       array('<br/>','</p><p class="content">'),
		       $input);

  return $parsed;
}

// parsed all newlines as multi ones, resulting in a many </p><p>s --
// so, only the first one seems to be matched...almost as if one linebreak in
// the browser seemed like multiple ones in the string
// browser is a mozilla 0.9.8 on the server -- shouldn't it just send \012
// using a linux-based system?

parseUserTextfield2($input) {
$parsed=preg_replace(array("/(\012\015|\012|\015)(\012\015|\012|\015)+/","/(\012\015|\012|\015)/"),
		       array('</p><p class="content">','<br/>'),
		       $input);

  return $parsed;
}

// curious thing: if i replace the linebreak expr. w/ BREAK as in:
function parseUserTextfield($input) {
  $parsed=preg_replace(array("/(BREAK)(BREAK)+/","/(BREAK)/"),
		       array('</p><p class="content">','<br/>'),
		       $parsed);

  return $parsed;
}
// everything works as expected
------------------------

List of Modules // ./configure - line (this simply is the SuSE7.3 i386 Cfg with security updates):

'./configure' '--prefix=/usr/share' '--datadir=/usr/share/php' '--bindir=/usr/bin' '--libdir=/usr/share' '--with-config-file-path=/etc' '--with-exec-dir=/usr/lib/php/bin' '--with-mysql=/usr' '--with-gd=yes' '--enable-gd-native-ttf' '--enable-gd-imgstrttf' '--with-tiff-dir=/usr' '--with-jpeg-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr/X11R6' '--with-ldap=yes' '--with-zlib=yes' '--with-bz2' '--with-gmp' '--with-xml' '--with-dom' '--with-ttf' '--with-t1lib' '--with-mcal=/usr' '--with-imap=yes' '--with-sablot' '--with-readline' '--with-ftp' '--with-ndbm' '--with-gdbm' '--with-mcrypt' '--with-gettext' '--with-mm' '--with-gd=yes' '--with-qtdom=/usr/lib/qt' '--enable-versioning' '--enable-yp' '--enable-bcmath' '--enable-trans-sid' '--enable-inline-optimization' '--enable-track-vars' '--enable-magic-quotes' '--enable-safe-mode' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-shmop' '--enable-calendar' '--enable-mbstring' '--enable-mbstr-enc-trans' '--enable-exif' '--enable-ftp' '--enable-memory-limit' '--enable-wddx' '--enable-filepro' '--enable-dbase' '--enable-ctype' '--disable-debug' '--enable-force-cgi-redirect' '--enable-discard-path' '--enable-sigchild' '--with-openssl' '--with-curl' '--with-swf=./dist/' '--with-imap-ssl' '--with-gd=yes' '--with-apxs=/usr/sbin/apxs' '--with-pgsql=/usr' '--with-snmp' 'i386-suse-linux'

Other Info:
None that I know of.

Backtrace:
No crash, no trace.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-04-09 04:17 UTC] sander@php.net
The bug system is not the appropriate forum for asking support
questions. For a list of a range of more appropriate places to ask
for help using PHP, please visit http://www.php.net/support.php

Hint: IIRC, PHP converts all newlines to \n so you can use \n in your regexes.
 [2002-04-09 10:24 UTC] haug at berndhaug dot net
My stupid: Windows linebreak ist \015\012, so curious things were bound to happen.
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Wed Dec 08 07:03:33 2021 UTC