Page 1 of 1
regex extraction of string data
Posted: Mon Dec 04, 2006 6:52 am
by mattcooper
OK, I'm hideously confused by regex, I admit it, so I need a helping hand here.
Can anyone provide me with a regular expression that would match the following string?
Code: Select all
{-- dynamic_object [object_name] --}
Where "dynamic_object" will always be the same, but "object_name" will always vary? There will be several instances of this in a string, and I need to be able to find out what "object_name" is so that I can put it in a database - it's for a templating system.
I'm having little success using strpos(), so regex it has to be, I guess!
All help seriously appreciated

Posted: Mon Dec 04, 2006 9:09 am
by Corvin
Code: Select all
<?php
$text = "{-- dynamic_object [object_name] --}";
preg_replace("/\{-- dynamic_object \[(.*)\] --\}/isUe", "get_object_name('\\1')", $text);
function get_object_name($name) {
echo $name;
return true;
}
?>
Posted: Mon Dec 04, 2006 9:49 am
by mattcooper
Fantastic! This finds all of the object names in my template, which is a great start. Thank you. However, it only echoes out the returned string, and I can't seem to get the object names into an array with the function.
If I attempt to load $name into an array, print_r returns null.
Seems to be the beginnings of a robust solution for this prob, thanks again and any further input would be GREATLY appreciated

Posted: Mon Dec 04, 2006 9:53 am
by Corvin
What did you try ? Show me your PHP-Code.

Posted: Mon Dec 04, 2006 10:01 am
by mattcooper
This is what I originally tried:
Code: Select all
$text = "{-- dynamic_object [heading_text] --}mnmn{-- dynamic_object [sidebar] --}kjkkkjkkjkj{-- dynamic_object [body_content] --}";
preg_replace("/\{-- dynamic_object \[(.*)\] --\}/isUe", "get_object_name('\\1')", $text);
$object_arr = array();
function get_object_name($name) {
$object_arr[] = $name;
return true;
}
print_r(get_object_name($name));
print_r($object_arr); ############ neither of these work
Instead of echoing $name, I tried to put it into an array - even if it were only one value (the whole string), I'd have been able o do something with it.
I can echo $name."/", which gives me a string of object names seperated by a trailing slash. However, I need the data to be assigned to a variable or stored in an array befre I can do anything with it. Follow me?
Posted: Mon Dec 04, 2006 10:11 am
by Corvin
You had some trouble with your var names and you've to use
global in the funtion. This should work:
Code: Select all
<?php
$object_arr = array();
$text = "{-- dynamic_object [heading_text] --}mnmn{-- dynamic_object [sidebar] --}kjkkkjkkjkj{-- dynamic_object [body_content] --}";
preg_replace("/\{-- dynamic_object \[(.*)\] --\}/isUe", "get_object_name('\\1')", $text);
function get_object_name($name) {
global $object_arr;
$object_arr[] = $name;
return true;
}
print_r($object_arr);
?>
Posted: Mon Dec 04, 2006 10:13 am
by mattcooper

Yes, I see where I went wrong! It works fine now.
Many thanks, I can now crack on and finish up this project. Mist learn some regex...
Good luck

Posted: Mon Dec 04, 2006 10:31 am
by sweatje
If you just need to know what they are in order to do something with them in the database, I am confused as to why you are using preg_replace.
Here is an example, expressed as a
SimpleTest test case:
Code: Select all
function testRegexToFindObjectStrings() {
$str = 'blah{-- dynamic_object [object_name1] --}
foo {-- dynamic_object [object_name2] --} bar {-- dynamic_object [object_name3] --}';
preg_match_all('~\{-- dynamic_object \[([^\]]+)\] --\}~ims', $str, $match);
$this->assertEqual(
array('object_name1','object_name2','object_name3')
,$match[1]);
}
Posted: Mon Dec 04, 2006 10:41 am
by mattcooper
Understandable, since my interaction with the database was not explained.
These dynamic object tags do two things. Firstly, they tell PHP that it should be replacing an instance of this tag with a function call (to populate the editable regoins of the page), and secondly to tell PHP which object, by name, it should be placing there.
So, if we come across a tag that reads "{-- dynamic_object [body_content] --}, we know that "dynamic_object" is an editable region (as opposed to , say, {-- navigation_object [section_navigation] --}), and that the page should be displaying the content stored in the database wherever "$row[page_id] == $page_id AND object_name = body_content".
So, there's no need to do any more than what corvin has done, really.
This make sense?