Solved

How to display part of the string?

Posted on 2011-03-23
6
304 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AdminLTE with wordpress 15 52
Really simple no curl. Send a post 3 times 4 25
How can I make this form submit to itself? 10 26
How is this connection happening? 3 11
This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

776 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