Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Extract part of a string (difficult)
Old 10-28-2007, 04:43 PM Extract part of a string (difficult)
Junior Talker

Posts: 1
Trades: 0
Hi,

I am looking for a code that can grab everything in between the brackets corresponding to a certain variable ($name).

For example, if my string is

$string =
"John{
john is a brown worm
}

Laura{
nancy is a blue pig
}";

and $name = "John", what I want returned is "john is a brown worm";
$name can equal Laura too, or any other name, in a list of n names, so John doesn't have to be the first name.
And the contents within the brackets can span multiple lines.

I am trying to do it with preg_match but it's not working.

Thanks for your help!

Last edited by alste450; 10-28-2007 at 05:19 PM..
alste450 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-28-2007, 05:35 PM Re: Extract part of a string (difficult)
joder's Avatar
Flipotron

Posts: 6,442
Name: James
Location: In the ocean.
Trades: 0
http://www.php.net/manual/en/function.str-replace.php
joder is offline
Reply With Quote
View Public Profile
 
Old 10-29-2007, 02:55 AM Re: Extract part of a string (difficult)
Insensus's Avatar
Ultra Talker

Posts: 487
Name: Mark Stegeman
Location: Netherlands, Europe
Trades: 0
Using preg_match is the right way, but it's quite hard. Especially with newlines.

Try this:

preg_match(
'/'.$name.'\{\s\s(.*)\s\s}/',
$fulltext,
$matches);
__________________
<?php ($helpfull>0)?$talkupation++ : '';?>
Insensus is offline
Reply With Quote
View Public Profile
 
Old 10-29-2007, 05:56 AM Re: Extract part of a string (difficult)
mtishetsky's Avatar
King Spam Talker

Posts: 1,226
Name: Mike
Location: Mataro, Spain
Trades: 0
Using preg_match may require setting proper modifier, in particular /s to make . match any symbol including newlines. RTFM about pattern modifiers.
__________________

Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE
-
Please login or register to view this content. Registration is FREE

And don't forget to give me talkupation!
mtishetsky is offline
Reply With Quote
View Public Profile Visit mtishetsky's homepage!
 
Reply     « Reply to Extract part of a string (difficult)
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

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





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14958 seconds with 12 queries