Extracting data from a string without explode()?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

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

Extracting data from a string without explode()?

Post by mattcooper »

Hello all,

I have a CMS template that contains tags in the following format:

{-- dynamic_object [objectname] --}

The 'objectname' needs to be extracted and put into a database when a new page is created so that the CMS knows what objects to place on live pages. The number of these tags and 'objectname' will differ from template to template.

I've tried using explode() (first on '{-- dynamic_object [', and then on '] --}' in the resultant array, which I tried to convert back to a string) to extract 'objectname', but haven't achieved much apart from being slightly confused.

Does anyone know of an easier way to do this?

Thanks in advance,

Matt
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

if the name of your object and key will be the same length for all of them (not likely) you could use substr(). Otherwise, you'll probably need a regular expression.

edit: ahh yes...didn't think of using strpos() ... good call
Last edited by Burrito on Sat Dec 02, 2006 3:02 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

loop { 2 × strpos() + substr() }
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Post by mattcooper »

We're on the right lines. This function works in a limited way:

Code: Select all

function find_object($data_source, $start, $end, $start_position) {
		
		$start_pos = strpos($data_source, $start, $start_position);
		$middle_pos = $beginning_pos + strlen($beginning);
		$end_pos = strpos($data_source, $end, $start_pos + 1);
		$object = substr($data_source, $middle_pos, $end_pos - $middle_pos);
		return $object;
		
}
This

Code: Select all

$object = find_object($template_data, '{-- dynamic_object [', '] --}', 0);
returns the name of the first object in the template. If I put it into a loop that runs for the number of times set by count() of the array from explode(), I get the same object name x number of times.

How do I move the function on to the next object when I don't know where it is in the string?

Thanks for your time and help, and credit to the chap who posted this function (which I've altered a bit) on PHP.net.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it'd be something like

Code: Select all

while first_needle in string from previous position
  update previous position via finding position of second_needle
  add the substring between the first_needle and second_needle to an array
Post Reply