Solved

Get contents from text file

Posted on 2006-07-21
3
178 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
ID: 17156454
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
ID: 17159597
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
ID: 17159599
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

815 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

8 Experts available now in Live!

Get 1:1 Help Now