1

this is my code:

    $searchfor = array();
    $searchfor[0] = 'INT.';
    $searchfor[1] = 'EXT.';

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);
    // escape special characters in the query
    $pattern = preg_quote($searchfor[0], '/');
    // finalize the regular expression, matching the whole line
    $pattern = "/^.*$pattern.*\$/m";
    // search, and store all matching occurences in $matches
    if(preg_match_all($pattern, $contents, $matches)){
        echo "<div class='int'>".implode("\n", $matches[0])."</div>\n";
    }else{
        echo "No matches found";
    }

How can i take the results and out each array item into an array of DIVs?

right now the output looks like this

<div class="int">INT 1</div>
INT 2

but I have like 150 instances of where INT. is found in my file so I was wanting to get it to show like this:

<div class="int" id="1">INT 1</div>
<div class="int" id="2">INT 2</div>
<div class="int" id="3">INT 3</div>
<div class="int" id="4">INT 4</div>
<div class="int" id="5">INT 5</div>
<div class="int" id="6">INT 6</div>
<div class="int" id="7">INT 7</div>
<div class="int" id="8">INT 8</div>

and so on...

Any suggestions?

UPDATED CODE:

    echo '<div id="int-div" style="width: 738px; height: 100%; border: 1px solid #000; padding: 5px; background-color: #FFF;">';
    $searchfor = array();
    $searchfor[0] = 'INT.';
    $searchfor[1] = 'EXT.';
    $searchfor[2] = 'I/E.';

    // get the file contents, assuming the file to be readable (and exist)
    $contents = file_get_contents($file);
    // escape special characters in the query
    $pattern = preg_quote($searchfor[0], '/');
    // finalize the regular expression, matching the whole line
    $pattern = "/^.*$pattern.*\$/m";
    // search, and store all matching occurences in $matches
    if(preg_match_all($pattern, $contents, $matches)){

        $row = 0;
        foreach($matches as $match)
        {
            echo "<ol><li><div class='int' id='".$row."'>".implode("</div></li><li><div class='int' id='".$row."'>", $match)."</div></li></ol>\n"; 
            $row++;
        }
    }else{
        echo "No matches found";
    }
    echo '</div>';

UPDATES RESULTS:

<ol>
<li>
<div class="int" id="0">INT. STRAWBERRY'S BEDROOM - MORNING</div>
</li>
<li>
<div class="int" id="0">INT. PALO TORCIDO HIGH SCHOOL, CLASSROOM - MORNING</div>
</li>
<li>
<div class="int" id="0">INT. DUNKIN' DONUTS - MORNING</div>
</li>
<li>
<div class="int" id="0">INT. DUNKIN' DONUTS - MORNING</div>
<li>
<div class="int" id="0">INT. DUNKIN' DONUTS - EARLY MORNING (FLASHBACK)</div>
</li>
<li>
<div class="int" id="0">INT. FUENTES RESIDENCE, KITCHEN - NIGHT (PRESENT)</div>
</li>
<li>

2 Answers 2

3

use a foreach and use the keys as your id:

if(preg_match_all($pattern, $contents, $matches))
{
    $row = 0;
    foreach($matches as $match)
    {
        echo "<div class='int' id='".$row."'>".implode("\n", $match)."</div>\n";
        $row++;
    }
}else{
       echo "No matches found";
}
Sign up to request clarification or add additional context in comments.

9 Comments

oh, i did try a foreach loop but i forgot about they key value bit, well my loops was made a bit diff but i think i was in the right area. Um, just curious but it returns Array
it should have the exact same output as your original :/, var_dump($match); in your loop and update your post with the contents please.
i'm sorry, i got it to post as expected, Only problem is the $key is always at 0. I update with new code
$key can never always be 0, this would go against all programming laws, as $key is an index of the array they are unique, please post some sample html that was produced.
Updated to counter that,im not sure why this is happening but it must be from the regex :/
|
0

You can avoid messy quote escaping and concatenation by creating a template string and using printf(). Notice the placeholders and the two variables that relate to them.

Code: (Demo)

$matches = [
    [
        "INT. STRAWBERRY'S BEDROOM - MORNING",
        "INT. PALO TORCIDO HIGH SCHOOL, CLASSROOM - MORNING",
        "INT. DUNKIN' DONUTS - MORNING",
        "INT. DUNKIN' DONUTS - MORNING",
        "INT. DUNKIN' DONUTS - EARLY MORNING (FLASHBACK)",
        "INT. FUENTES RESIDENCE, KITCHEN - NIGHT (PRESENT)",
    ]
];

$liTemplate = <<<HTML
    <li><div class="int" id="%d">%s</div></li>

HTML;

if ($matches) {
    echo "<ol>\n";
    foreach ($matches[0] as $i => $match) {
        printf($liTemplate, $i + 1, $match);
    }
    echo "</ol>";
}

Output:

<ol>
    <li><div class="int" id="1">INT. STRAWBERRY'S BEDROOM - MORNING</div></li>
    <li><div class="int" id="2">INT. PALO TORCIDO HIGH SCHOOL, CLASSROOM - MORNING</div></li>
    <li><div class="int" id="3">INT. DUNKIN' DONUTS - MORNING</div></li>
    <li><div class="int" id="4">INT. DUNKIN' DONUTS - MORNING</div></li>
    <li><div class="int" id="5">INT. DUNKIN' DONUTS - EARLY MORNING (FLASHBACK)</div></li>
    <li><div class="int" id="6">INT. FUENTES RESIDENCE, KITCHEN - NIGHT (PRESENT)</div></li>
</ol>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.