Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

How to display part of the string?

Posted on 2011-03-23
6
Medium Priority
?
309 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 31

Accepted Solution

by:
Marco Gasi earned 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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!

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
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 how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

636 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