?
Solved

Regex for String

Posted on 2011-10-03
3
Medium Priority
?
284 Views
Last Modified: 2012-05-12
In the following string I would like to get the following:

Get this:

Business Machine.\nCombustion.\nFuel Cell.\nIndustrial.\nMedical.\nTransit.\nAquaculture.\nOther.

From this:

string(264) "required=0 changeable=0 show_at_reg_form=0 show_at_my_details_form=0 show_at_userlist_profile=0 show_at_userlist_card=0 values=Business Machine.\nCombustion.\nFuel Cell.\nIndustrial.\nMedical.\nTransit.\nAquaculture.\nOther. default_values= searcheable=1 cols=20 " 
<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

Open in new window

Thank you.
0
Comment
Question by:EMB01
3 Comments
 
LVL 17

Assisted Solution

by:Chris Harte
Chris Harte earned 200 total points
ID: 36902919
I do not do regex, but if your structure of the string is always the same you could do something like this;
$whole_string = "required=0 changeable=0 show_at_reg_form=0 show_at_my_details_form=0 show_at_userlist_profile=0 show_at_userlist_card=0 values=Business Machine.\nCombustion.\nFuel Cell.\nIndustrial.\nMedical.\nTransit.\nAquaculture.\nOther. default_values= searcheable=1 cols=20";

$start_string = strstr($whole_string, "default_value", true);

$end_string = strstr($start_string, "Business");

print_r($end_string);

Open in new window

0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1800 total points
ID: 36902920
How about this:

if (preg_match("/(?<=values=)([^.]|\.(?!\s*\w+=))+\./", $source, $match)) {
	echo $match[0];
}

Open in new window

0
 
LVL 14

Author Closing Comment

by:EMB01
ID: 36903010
I didn't test the non-regex solution, but the regex worked, thanks.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
It’s a season to be thankful, and we’re thankful for users like you who engage on site, solve technology problems, and network with others in the industry. What tech are we most thankful for? Keep reading.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month16 days, 16 hours left to enroll

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question