|
List Info
Thread: Basic html regex
|
|
| Basic html regex |

|
2006-06-27 18:59:47 |
I'm looking in PHP to replace:
<author>$foo</author>
with
<h2><a
href="profile.php?id=$id">$foo</a></
h2>
I guess I need a regex to find the string, then I need to
grab $foo and
look up $id in the database using it. Any ideas?
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Basic html regex |

|
2006-06-27 20:42:35 |
1. match '$foo' in '<author>$foo</author>'
with
(?<=<author>).*?(?=</author>)
2. trim '$foo' of any white-apaces
3. look-up in your DB to get a corresponding ID
4. construct the desired str '<h2><a
href="profile.php?id=$id">$foo</a></
h2>' from its parts
5. do replacement: this can be tricky; might need to use a
delegate to
do it (similar to .NET MatchEvaluator function). In this (or
similar)
case, u'll have ot do all the processing including a DB
call inside
your Regex.Replace() function.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Basic html regex |

|
2006-06-27 20:41:38 |
Matt wrote:
> I'm looking in PHP to replace:
>
> <author>$foo</author>
> with
> <h2><a
href="profile.php?id=$id">$foo</a></
h2>
>
> I guess I need a regex to find the string, then I need
to grab $foo and
> look up $id in the database using it. Any ideas?
you've almost got it, except that you need metaquotes with
your
variables in the pattern part..So, given any variable $foo,
you may
first check the corresponding $id from the database, like:
$id = check_your_database($foo);
and then
$newtext =
preg_replace("/<author>\Q$foo\E<\/author&g
t;/i", "<h2><a
href=\"profile.php?id=$id\">$foo</a>&
lt;/h2>", $oldtext);
But if you don't know the variable $foo beforehead, then
you may need
to use the function preg_replace_callback() instead.. for
example:
function toh2($matches)
{
$foo = matches[1];
$id = check_your_database($foo);
return "<h2><a
href=\"profile.php?id=$id\">$foo</a>&
lt;/h2>";
}
$newtext =
preg_replace_callback("/<author>(.*?)<\/auth
or>/i", "toh2",
$oldtext);
(untested)
Xicheng
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Basic html regex |

|
2006-06-27 20:50:54 |
Sergei Z wrote:
> 1. match '$foo' in
'<author>$foo</author>' with
>
> (?<=<author>).*?(?=</author>)
>
> 2. trim '$foo' of any white-apaces
>
> 3. look-up in your DB to get a corresponding ID
>
> 4. construct the desired str '<h2><a
>
href="profile.php?id=$id">$foo</a></
h2>' from its parts
>
> 5. do replacement: this can be tricky; might need to
use a delegate to
> do it (similar to .NET MatchEvaluator function). In
this (or similar)
> case, u'll have ot do all the processing including a
DB call inside
> your Regex.Replace() function.
Hi, Sergei:
I just checked several programming languages, like Perl,
.NET,
JavaScript, PHP, all of them support a function call in the
replacement
part(but the implementations are quite different.. )..and
Perl/PHP
also have /e modifier that makes things much easier.
BTW. I sent a PM to your account at RegexAdv.com. hope
you've got it.
and have a good day,
Xicheng
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
| Basic html regex |

|
2006-06-27 22:10:11 |
Xicheng Jia wrote:
> you've almost got it, except that you need metaquotes
with your
> variables in the pattern part..So, given any variable
$foo, you may
> first check the corresponding $id from the database,
like:
>
> $id = check_your_database($foo);
>
> and then
>
> $newtext =
preg_replace("/<author>\Q$foo\E<\/author&g
t;/i", "<h2><a
>
href=\"profile.php?id=$id\">$foo</a>&
lt;/h2>", $oldtext);
>
> But if you don't know the variable $foo beforehead,
then you may need
> to use the function preg_replace_callback() instead..
for example:
>
> function toh2($matches)
> {
> $foo = matches[1];
> $id = check_your_database($foo);
> return "<h2><a
href=\"profile.php?id=$id\">$foo</a>&
lt;/h2>";
> }
> $newtext =
preg_replace_callback("/<author>(.*?)<\/auth
or>/i", "toh2",
> $oldtext);
>
> (untested)
> Xicheng
Thank you very much, with a bit of modification this worked
perfectly,
thanks!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the
Google Groups "Regex" group.
To post to this group, send email to regex googlegroups.com
To unsubscribe from this group, send email to
regex-unsubscribe googlegroups.com
For more options, visit this group at http://groups.go
ogle.com/group/regex
-~----------~----~----~----~------~----~------~--~---
|
|
[1-5]
|
|
|
about | contact Other archives ( Real Estate discussion Medical topics )
|