Re: [PHP] Parsing brackets in text

2006-11-14 Thread Shuping Zhou

Actually, preg_replace() can solve Dotan Cohen's problem. There was just a
minor mistake in the code.

Please try this:

?php

function makeLink($title) {
   $returnString=b$title/b;
   return $returnString;
}

$articleText=This is a very [long] and [stupid] string.;
$articleText=preg_replace('/(\[[a-z]+\])/e', makeLink('$1'),
$articleText);
print $articleText;

?


Re: [PHP] Parsing brackets in text

2006-11-13 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-11-13 01:35:44 +0200:
 On 13/11/06, Chris [EMAIL PROTECTED] wrote:
 
 ?php
 function makeLink($matches) {
 $returnString=b . $matches[1] . /b;
 return $returnString;
 }
 
 $articleText=This is a very [long] string.;
 $articleText=preg_replace_callback('/\[([a-z]+)\]/i' , makeLink,
 $articleText);
 print $articleText . \n;
 ?
 
 
 The callback takes whatever the regular expressions returns (alpha
 characters between [ and ]) and runs it through function makeLink.
 
 Not sure how that will go with multiple []'s etc in the same string but
 it should get you started.
 
 
 I didn't know that I had to call the function like that- I don't
 remember ever seeing it called that way.

It's done this way in all the examples on
http://cz.php.net/manual/en/function.preg-replace-callback.php
What documentation did you read?

 Also, I must google the meaning of the (~+) in the regex.

No need to google:

http://cz.php.net/manual/en/reference.pcre.pattern.syntax.php

Skip the Differences From Perl section.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

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



Re: [PHP] Parsing brackets in text

2006-11-13 Thread Dotan Cohen

On 13/11/06, Roman Neuhauser [EMAIL PROTECTED] wrote:

# [EMAIL PROTECTED] / 2006-11-13 01:35:44 +0200:
 On 13/11/06, Chris [EMAIL PROTECTED] wrote:
 
 ?php
 function makeLink($matches) {
 $returnString=b . $matches[1] . /b;
 return $returnString;
 }
 
 $articleText=This is a very [long] string.;
 $articleText=preg_replace_callback('/\[([a-z]+)\]/i' , makeLink,
 $articleText);
 print $articleText . \n;
 ?
 
 
 The callback takes whatever the regular expressions returns (alpha
 characters between [ and ]) and runs it through function makeLink.
 
 Not sure how that will go with multiple []'s etc in the same string but
 it should get you started.
 

 I didn't know that I had to call the function like that- I don't
 remember ever seeing it called that way.

It's done this way in all the examples on
http://cz.php.net/manual/en/function.preg-replace-callback.php
What documentation did you read?


In the first example on that page, it looks to me like it should just
return the text next_year, and not call that function. Obviously, it
is my misunderstanding of the way that the funciton works, and not in
the function itself. I do learn php from TFM and this list, not from a
course. Just like in any learning environment, I could misinterpret
what is taught to me and need to ask a question, or not even know that
I misinterpreted and not know that I need to ask a question. This is a
case of the latter, clearly, because I have never needed to do
anything other than a simple str_replace until now.


 Also, I must google the meaning of the (~+) in the regex.

No need to google:

http://cz.php.net/manual/en/reference.pcre.pattern.syntax.php

Skip the Differences From Perl section.


That I have read at least three or four times. I still don't capture
all of it, but I try. You are correct, I should have referenced that
right away. It won't hurt for me to read it again, too. Thank you.

Dotan Cohen

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



Re: [PHP] Parsing brackets in text

2006-11-13 Thread Roman Neuhauser
# [EMAIL PROTECTED] / 2006-11-13 15:02:53 +0200:
 http://cz.php.net/manual/en/reference.pcre.pattern.syntax.php
 
 Skip the Differences From Perl section.
 
 That I have read at least three or four times. I still don't capture
 all of it, but I try. You are correct, I should have referenced that
 right away. It won't hurt for me to read it again, too. Thank you.

Just keep reading it, but not without trying out various things
yourself. I had hard time hammering it into my head a few years ago,
but it has been paying off like crazy. Your gain might vary.

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

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



[PHP] Parsing brackets in text

2006-11-12 Thread Dotan Cohen

I need to replace text enclosed in brackets with links. How can I get
that text, though? I've tried numerous variations of the following,
with no success:
$articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText);

I cannot get the text between the brackets send to the makeLink
function. Could someone push me in the right direction? An example of
the text would be:
$articleText=Ajax is an acronym of Asynchronous [JavaScript] and [XML];

Where I'd like the strings JavaScript and XML sent to the makeLink
function. Thanks in advance.

Dotan Cohen

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



Re: [PHP] Parsing brackets in text

2006-11-12 Thread Chris

Dotan Cohen wrote:

I need to replace text enclosed in brackets with links. How can I get
that text, though? I've tried numerous variations of the following,
with no success:
$articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText);

I cannot get the text between the brackets send to the makeLink
function. Could someone push me in the right direction? An example of
the text would be:
$articleText=Ajax is an acronym of Asynchronous [JavaScript] and [XML];

Where I'd like the strings JavaScript and XML sent to the makeLink
function. Thanks in advance.


Don't know if this is your problem but you need to escape the trailing ] 
as well:


preg_replace('/\[[a-z]\]/i', .


--
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] Parsing brackets in text

2006-11-12 Thread Dotan Cohen

On 12/11/06, Chris [EMAIL PROTECTED] wrote:

Dotan Cohen wrote:
 I need to replace text enclosed in brackets with links. How can I get
 that text, though? I've tried numerous variations of the following,
 with no success:
 $articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText);

 I cannot get the text between the brackets send to the makeLink
 function. Could someone push me in the right direction? An example of
 the text would be:
 $articleText=Ajax is an acronym of Asynchronous [JavaScript] and [XML];

 Where I'd like the strings JavaScript and XML sent to the makeLink
 function. Thanks in advance.

Don't know if this is your problem but you need to escape the trailing ]
as well:

preg_replace('/\[[a-z]\]/i', .



Thanks, but that wasn't quite it, either. This code:

?php
function makeLink($title) {
   $returnString=b$title/b;
   return $returnString;
}

$articleText=This is a very [long] string.;
$articleText=preg_replace('/\[[a-z]\]/i' , makeLink($1), $articleText);
print $articleText;
?

produces this error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
or '$' in /home/user/public_html/testpage.php on line 9

I'm sorry that I didn't include the error message in my original
email. Also, I got the same error with and without the last closing
bracket escaped.

Dotan Cohen

http://simplesniff.com/

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



Re: [PHP] Parsing brackets in text

2006-11-12 Thread Dotan Cohen

On 13/11/06, Chris [EMAIL PROTECTED] wrote:

Dotan Cohen wrote:
 On 12/11/06, Chris [EMAIL PROTECTED] wrote:
 Dotan Cohen wrote:
  I need to replace text enclosed in brackets with links. How can I get
  that text, though? I've tried numerous variations of the following,
  with no success:
  $articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText);
 
  I cannot get the text between the brackets send to the makeLink
  function. Could someone push me in the right direction? An example of
  the text would be:
  $articleText=Ajax is an acronym of Asynchronous [JavaScript] and
 [XML];
 
  Where I'd like the strings JavaScript and XML sent to the makeLink
  function. Thanks in advance.

 Don't know if this is your problem but you need to escape the trailing ]
 as well:

 preg_replace('/\[[a-z]\]/i', .


 Thanks, but that wasn't quite it, either. This code:

 ?php
 function makeLink($title) {
$returnString=b$title/b;
return $returnString;
 }

 $articleText=This is a very [long] string.;
 $articleText=preg_replace('/\[[a-z]\]/i' , makeLink($1), $articleText);
 print $articleText;
 ?

 produces this error:
 Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
 or '$' in /home/user/public_html/testpage.php on line 9

 I'm sorry that I didn't include the error message in my original
 email. Also, I got the same error with and without the last closing
 bracket escaped.

Ah, completely missed that part. You can't pass $1 through like that.

You have to do some funky quoting to do what you want in one go.

http://php.net/preg_replace

Check Example 4. Using the 'e' modifier

or use a callback:

http://www.php.net/manual/en/function.preg-replace-callback.php


I had looked at both of those already- with neither luck nor
understanding. I can fuddle my way through a simple regex, but that
monster has me bewildered. I'm pretty sure that preg_replace is all I
need, but I can't figure out the correct syntax for referencing a
chunk of the data.

I'm of course not asking anybody to write the regex for me, rather to
show me how it must be so that I'll know how to construct them. I saw
another, similar example on ilovejackdaniels.com where the author had
parsed Google query strings, but that example was even more confounded
than the one in TFphpM.

Dotan Cohen

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



Re: [PHP] Parsing brackets in text

2006-11-12 Thread Chris

Dotan Cohen wrote:

On 12/11/06, Chris [EMAIL PROTECTED] wrote:

Dotan Cohen wrote:
 I need to replace text enclosed in brackets with links. How can I get
 that text, though? I've tried numerous variations of the following,
 with no success:
 $articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), $articleText);

 I cannot get the text between the brackets send to the makeLink
 function. Could someone push me in the right direction? An example of
 the text would be:
 $articleText=Ajax is an acronym of Asynchronous [JavaScript] and 
[XML];


 Where I'd like the strings JavaScript and XML sent to the makeLink
 function. Thanks in advance.

Don't know if this is your problem but you need to escape the trailing ]
as well:

preg_replace('/\[[a-z]\]/i', .



Thanks, but that wasn't quite it, either. This code:

?php
function makeLink($title) {
   $returnString=b$title/b;
   return $returnString;
}

$articleText=This is a very [long] string.;
$articleText=preg_replace('/\[[a-z]\]/i' , makeLink($1), $articleText);
print $articleText;
?

produces this error:
Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
or '$' in /home/user/public_html/testpage.php on line 9

I'm sorry that I didn't include the error message in my original
email. Also, I got the same error with and without the last closing
bracket escaped.


Ah, completely missed that part. You can't pass $1 through like that.

You have to do some funky quoting to do what you want in one go.

http://php.net/preg_replace

Check Example 4. Using the 'e' modifier

or use a callback:

http://www.php.net/manual/en/function.preg-replace-callback.php


--
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] Parsing brackets in text

2006-11-12 Thread Chris

Dotan Cohen wrote:

On 13/11/06, Chris [EMAIL PROTECTED] wrote:

Dotan Cohen wrote:
 On 12/11/06, Chris [EMAIL PROTECTED] wrote:
 Dotan Cohen wrote:
  I need to replace text enclosed in brackets with links. How can I 
get

  that text, though? I've tried numerous variations of the following,
  with no success:
  $articleText=preg_replace('/\[[a-z]]/i' , makeLink($1), 
$articleText);

 
  I cannot get the text between the brackets send to the makeLink
  function. Could someone push me in the right direction? An 
example of

  the text would be:
  $articleText=Ajax is an acronym of Asynchronous [JavaScript] and
 [XML];
 
  Where I'd like the strings JavaScript and XML sent to the 
makeLink

  function. Thanks in advance.

 Don't know if this is your problem but you need to escape the 
trailing ]

 as well:

 preg_replace('/\[[a-z]\]/i', .


 Thanks, but that wasn't quite it, either. This code:

 ?php
 function makeLink($title) {
$returnString=b$title/b;
return $returnString;
 }

 $articleText=This is a very [long] string.;
 $articleText=preg_replace('/\[[a-z]\]/i' , makeLink($1), $articleText);
 print $articleText;
 ?

 produces this error:
 Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE
 or '$' in /home/user/public_html/testpage.php on line 9

 I'm sorry that I didn't include the error message in my original
 email. Also, I got the same error with and without the last closing
 bracket escaped.

Ah, completely missed that part. You can't pass $1 through like that.

You have to do some funky quoting to do what you want in one go.

http://php.net/preg_replace

Check Example 4. Using the 'e' modifier

or use a callback:

http://www.php.net/manual/en/function.preg-replace-callback.php


I had looked at both of those already- with neither luck nor
understanding. I can fuddle my way through a simple regex, but that
monster has me bewildered. I'm pretty sure that preg_replace is all I
need, but I can't figure out the correct syntax for referencing a
chunk of the data.

I'm of course not asking anybody to write the regex for me, rather to
show me how it must be so that I'll know how to construct them. I saw
another, similar example on ilovejackdaniels.com where the author had
parsed Google query strings, but that example was even more confounded
than the one in TFphpM.


?php
function makeLink($matches) {
$returnString=b . $matches[1] . /b;
return $returnString;
}

$articleText=This is a very [long] string.;
$articleText=preg_replace_callback('/\[([a-z]+)\]/i' , makeLink, 
$articleText);

print $articleText . \n;
?


The callback takes whatever the regular expressions returns (alpha 
characters between [ and ]) and runs it through function makeLink.


Not sure how that will go with multiple []'s etc in the same string but 
it should get you started.


--
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] Parsing brackets in text

2006-11-12 Thread Dotan Cohen

On 13/11/06, Chris [EMAIL PROTECTED] wrote:


?php
function makeLink($matches) {
$returnString=b . $matches[1] . /b;
return $returnString;
}

$articleText=This is a very [long] string.;
$articleText=preg_replace_callback('/\[([a-z]+)\]/i' , makeLink,
$articleText);
print $articleText . \n;
?


The callback takes whatever the regular expressions returns (alpha
characters between [ and ]) and runs it through function makeLink.

Not sure how that will go with multiple []'s etc in the same string but
it should get you started.



I didn't know that I had to call the function like that- I don't
remember ever seeing it called that way. Also, I must google the
meaning of the (~+) in the regex.

Thank you very much, Chris. I very much appreciate the assistance.
Know that I am learning, not just asking for hand-holding!

Dotan Cohen

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