Solved

How to display part of the string?

Posted on 2011-03-23
6
303 Views
Last Modified: 2012-08-13
Hi,

I have string like so:
$data = "part1: testing
part2: piece";

Open in new window

And I want display 'testing' only, more specificly content between 'part1: ' and 'part2: ', how to do this?

Thanks for any help.
0
Comment
Question by:Zado
  • 3
  • 3
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 500 total points
ID: 35199923
Try this:

$data = "part1: testing
part2: piece";
preg_match('/(?<=part1:).*(?=part2)/si', $data, $match);
echo $match[0];

Cheers
0
 
LVL 8

Author Comment

by:Zado
ID: 35200132
Thanks, it works for
$data = "part1: testing
part2: piece";

Open in new window


but not for long string like this:
$data = "prefix: aus
chart number: 14
suffix: b
international number: xx
class: xx
p panda na: P&A
ba folio number: 38
arcs on cd: xx
arcs region: RC5
chart title: Chagos Archipelago
chart scale: 360,000
chart vertex: xx
new chart date: xx
new edition date: 05/03/1998
previous edition date: xx
withdrawn date: xx
cancel date: xx
replaced by chart number: xx";

Open in new window

I tried the following code for string above:
preg_match('/(?<=prefix:).*(?=chart)/si', $data, $match);

Open in new window

or
preg_match('/(?<=prefix:).*(?=chart number)/si', $data, $match);

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35200204
This works on my system:

$data = "prefix: aus
chart number: 14
suffix: b
international number: xx
class: xx
p panda na: P&A
ba folio number: 38
arcs on cd: xx
arcs region: RC5
chart title: Chagos Archipelago
chart scale: 360,000
chart vertex: xx
new chart date: xx
new edition date: 05/03/1998
previous edition date: xx
withdrawn date: xx
cancel date: xx
replaced by chart number: xx";
preg_match('/(?<=prefix:).*(?=chart)/si', $data, $match);
echo $match[0];

What you get and what you would to get?
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 8

Author Comment

by:Zado
ID: 35200207
Ok, got it, just changed 'chart number' to 'chart_number'
0
 
LVL 8

Author Comment

by:Zado
ID: 35200218
Your code displays this:

aus chart number: 14 suffix: b international_number: xx class: xx p panda na: P&A ba folio number: 38 arcs on cd: xx arcs region: RC5 chart title: Chagos Archipelago chart scale: 360,000 chart vertex: xx new chart date: xx new edition date: 05/03/1998 previous edition date: xx withdrawn date: xx cancel date: xx replaced by

don't know why, probably different version of php. But I fixed it so it's ok now. Thanks for your help.
0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 35200239
Glad to helped you. Bye
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

911 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now