PDA

View Full Version : [PHP] Obtain Data from String



nobody
March 26th, 2008, 05:42 PM
I'm sure there's a name for what I'm trying to do, but I'm at a loss here.

So I'm working on a text replacement script for something I'm working on. Basically I'll have something like this:

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec nisi lacus, egestas sed, hendrerit id, gravida iaculis, dolor.{a409ndk32|small|left|Hey There} Aliquam consectetuer, turpis in sollicitudin sodales, massa eros cursus turpis, in vulputate diam nisi eget erat. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia Curae; Aenean mollis mi nec velit. Nam pellentesque nisi vitae ipsum. Proin risus.{sd90f32kd|large|right|What Up?} Donec tristique felis nec augue. Nulla arcu elit, condimentum sit amet, rhoncus sit amet, consectetuer ac, tortor.

Notice the information in the brackets (such as {a409ndk32|small|left|Hey There}). Somehow I need to strip out each of the four items and be able to manipulate them. Perhaps pull them out as an array because it could be 1 to 100 of these at any time.

I assume this is a regex thing and unfortunately I don't know anything about regex.

Any thoughts or help?

simplistik
March 26th, 2008, 08:08 PM
Well the regex you'll need goes something like:


<?php
$str = "{a409ndk32|small|left|Hey There}";
$pattern = '/{([\s\S]+?)}/';
preg_match($pattern, $str, $matches, PREG_OFFSET_CAPTURE);
?>

nobody
March 26th, 2008, 08:29 PM
What will that return?

joran420
March 26th, 2008, 08:58 PM
try it and trace $matches

simplistik
March 27th, 2008, 11:28 AM
if you do a


print_r($matches[1]);

it should return


a409ndk32|small|left|Hey There