• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 311
  • Last Modified:

How to display part of the string?

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
Zado
Asked:
Zado
  • 3
  • 3
1 Solution
 
Marco GasiFreelancerCommented:
Try this:

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

Cheers
0
 
ZadoAuthor Commented:
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
 
Marco GasiFreelancerCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
ZadoAuthor Commented:
Ok, got it, just changed 'chart number' to 'chart_number'
0
 
ZadoAuthor Commented:
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
 
Marco GasiFreelancerCommented:
Glad to helped you. Bye
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now