0

Here is the text value i am trying to search, this text don't have ant HTML in it( thats th problem HTML DOM not working)

    User Guide
    For iOS 7.1 Software
    Contents
    Chapter 1: New One

    iPhone at a Glance
    iPhone
    overview
    Accessories
    Multi-Touch screen
    Buttons
    Status icons
    Chapter 2 Second One this is long
    Chapter 3 new this is long

Ok now i am trying to get Chapter 1: New One and Chapter 2 Second One this is long kind of values, there are more Chapter to get.

I was trying PHP Simple HTML DOM, but don't know how to extract these Chapters with different format and length.

2 Answers 2

2

Did you mean something like this ...?

<?php

$lines = "    User Guide
    For iOS 7.1 Software
    Contents
    Chapter 1: New One

    iPhone at a Glance
    iPhone
    overview
    Accessories
    Multi-Touch screen
    Buttons
    Status icons
    Chapter 2 Second One this is long
    Chapter 3 new this is long";

$lines = explode("\r\n", $lines);

foreach ($lines as $line) {
    $line = trim($line);
    if (!empty($line)) {
        if (preg_match('/Chapter \\d/', $line)) {
            echo $line ."<br>";
        }
    }
}

Outputs:

Chapter 1: New One
Chapter 2 Second One this is long
Chapter 3 new this is long
Sign up to request clarification or add additional context in comments.

Comments

1

There's no DOM so using methods for that won't help. You can use array_filter and explode.

$chapters = array_filter(explode("\r\n", $lines), function ($line) {
  $line = trim($line);
  return substr($line, 0, 7) === 'Chapter';
});

Then $chapters should look something like this:

array(
  "Chapter 1: New One",
  "Chapter 2 Second One this is long",
  "Chapter 3 new this is long"
);

My PHP is kind of rusty, but should get you close!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.