regex extraction of string data

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

regex extraction of string data

Post 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 ;)
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post 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;
}
?>
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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 :D
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post by Corvin »

What did you try ? Show me your PHP-Code. :)
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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?
Last edited by mattcooper on Mon Dec 04, 2006 10:12 am, edited 1 time in total.
Corvin
Forum Commoner
Posts: 49
Joined: Sun Dec 03, 2006 1:04 pm

Post 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);
?>
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

:oops: 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 :)
User avatar
sweatje
Forum Contributor
Posts: 277
Joined: Wed Jun 29, 2005 10:04 pm
Location: Iowa, USA

Post 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]);

}
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post 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?
Post Reply