Solved

Get contents from text file

Posted on 2006-07-21
3
174 Views
Last Modified: 2008-01-09
Hi Guys,

I have the text file which have the contents given below.

"<text title1="Training Area" title2="Test">
<p align="left"><b>HTML</b> Content for the training.<br>
This area will be filled with HTML source code to be displayed into the page.
</text>"

I want to get the contents only the attribute values of 'title1' and 'title2' in the text tag.

How to get this? Please provide me the solution for this.
0
Comment
Question by:gstalin
  • 2
3 Comments
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
If you want to use xml parser, then I would suggest using xml_unserialize from Keith Devens.

If you want to use preg_match you can use:
$title1 = '';
$title2 = '';
if(preg_match_all('/(title\d+)="(.*?)"/', $text, $results)) {
  foreach($results[0] as $key => $value) {
      if($results[1][$key] == 'title1') $title1 = $results[2][$key];
      if($results[1][$key] == 'title2') $title2 = $results[2][$key];
  }
}

-r-
0
 
LVL 2

Expert Comment

by:arun80_inin
Comment Utility
Very bad solution. If you want try this

$title2 = '';
$arr=explode("title1=\"",$str);
$arr=explode("\"",$arr[1]);
$title1=$arr[0];

$arr=explode("title2=\"",$str);
$arr=explode("\"",$arr[1]);
$title2=$arr[0];
echo "<br>title1=".$title1;
echo "<br>title2=".$title2;
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 500 total points
Comment Utility
Then you can better use:

$title1 = preg_match('title1="(.*?)"', $str, $m) ? $m[1] : '';
$title2 = preg_match('title2="(.*?)"', $str, $m) ? $m[1] : '';

-r-
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

763 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

10 Experts available now in Live!

Get 1:1 Help Now