Regular Expression Result as Associative Array Index

This is a discussion on Regular Expression Result as Associative Array Index within the PHP Language forums, part of the PHP Programming Forums category; Hi Everyone. $layout=ereg_replace("\[\[([a-z:0-9]+)\]\]", $dillo['\\1'], $layout); I'm trying to use the result ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-11-2008
skezzolo@gmail.com
 
Posts: n/a
Default Regular Expression Result as Associative Array Index

Hi Everyone.

$layout=ereg_replace("\[\[([a-z:0-9]+)\]\]", $dillo['\\1'], $layout);

I'm trying to use the result of a Regular Expression Match as an Index
for the Associative Array $Dillo[], so that the to-replace content can
be controlled from the to-be-replaced content.
Unfortunately the code above doesn't seem to work.
Any ideas?

Sorry about my poor english, i'm workin' on that.
skezzolo
Reply With Quote
  #2 (permalink)  
Old 05-12-2008
Álvaro G. Vicario
 
Posts: n/a
Default Re: Regular Expression Result as Associative Array Index

skezzolo@gmail.com escribió:
> $layout=ereg_replace("\[\[([a-z:0-9]+)\]\]", $dillo['\\1'], $layout);


The ereg_* functions will not be available in the PHP 6 core so you'd
better get used preg_* equivalents:

http://bitfilm.net/2007/09/21/becomi...-6-compatible/


> I'm trying to use the result of a Regular Expression Match as an Index
> for the Associative Array $Dillo[], so that the to-replace content can
> be controlled from the to-be-replaced content.


If you need a match then you must use preg_match() rather than
preg_replace().

Try this code:

<?php

$Dillo = array(
'alvaro:99' => 'Hello, world',
);
$layout = 'foo:bar[[alvaro:99]]';

if( preg_match('/\[\[([a-z:0-9]+)\]\]/', $layout, $matches) ){
$layout = $Dillo[$matches[1]];
}

echo $layout;

?>

Of course, you'd need some extra checks.



--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://bits.demogracia.com
-- Mi web de humor al baño María: http://www.demogracia.com
--
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:36 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0