Solved

How to display part of the string?

Posted on 2011-03-23
6
302 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 30

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 30

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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 30

Expert Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

707 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

12 Experts available now in Live!

Get 1:1 Help Now