How to copy a piece of text into a textarea

0

I have a text written in a <TextArea> on a html page and would like to read the text and remove a piece of it and write in another <TextArea> , I tried to use preg_match but I could not understand how it worked, let's go to data.

Text:

<html>
<head>
    <title>Teste</title>
</head>
<body>
    <h1> Hello World </h1>  
    <h2> está </h2>
    <h1> TUDO? </h1>     
</body>
</html>

Answer:

  

Hello World
  EVERYTHING?

I need to read whatever is between <h1> and </h1> .

Code:

<textarea name="resultcode" cols="130" rows="30" wrap="OFF">
<?php
    $html = $_POST['htmlcode'];
    echo preg_match_all('/<h1>(.*)</h1>(.*)/i',$html, $matches);
    var_dump($matches);
?>
</textarea>

ERROR:

<br />
<b>Warning</b>:  preg_match_all(): Unknown modifier 'h' in <b>/home/u982560592/public_html/index.php</b> on line <b>19</b><br />
NULL
    
asked by anonymous 31.08.2017 / 15:51

2 answers

1

Preg_match and preg_match_all use REGEX and return a boolean indicating whether the expression (default) was found. About Regular Expressions you can ask your questions here and test here / a>

As for the problem itself, you can do this:

<?php
// Pega o POST
$html = filter_input(INPUT_POST, 'htmlcode');

// Se encontrar algum <h1> no html...
if(preg_match('/<h1>(.*)<\/h1>/iU', $html, $match)){
    echo $match[1];
}

To get more than one occurrence, use preg_match_all , like this:

<?php
// Pega o POST
$html = filter_input(INPUT_POST, 'htmlcode');

// Se encontrar algum <h1> no html...
if(preg_match_all('/<h1>(.*)<\/h1>/iU', $html, $match)){
    // Pega as ocorrencias do conteúdo de <h1>
    $ocorrencias = $match[1];
    // Tem alguma ocorrência?
    if(!empty($ocorrencias)){
        // Lê uma a uma
        foreach($ocorrencias as $index => $ocorrencia){
            // Exibe cada uma das ocorrências do conteúdo de <h1>
            echo "$index - $ocorrencia <br>";
        }
    }
}

Explaining the modifiers

Note that in REGEX within preg_match() everything inside / is the default. what is outside are modifiers. The i f modifier indicates to ignore the case (lowercase) and the modifier U indicates that REGEX is not greedy, that is, in ". *" it will stop picking up when it finds the next pattern that is </h1> . Note tb that in <\/h1> has been escaped the bar for REGEX not to confuse with the closing bar.

You can see more about the modifiers here .

    
31.08.2017 / 16:10
-1

Do not parse HTML using preg_match .

Use the DOMDocument class

Example:

<?php 

$html= "<p>Olá!</p>
<h1>Titulo H1</h1>
<h2>Titulo H2</h2>
<h3>Titulo H3</h3>";
// Cria um novo objeto DOM
$dom = new domDocument('1.0', 'utf-8'); 
// Carrega o HTML no Objeto
$dom->loadHTML($html); 
//remove espaços em branco
$dom->preserveWhiteSpace = false; 
$hTwo= $dom->getElementsByTagName('h2'); // Defina aqui a sua tag desejada
echo $hTwo->item(0)->nodeValue; 
//Irá retornar "Titulo H2";
?>

If I realize I'm printing $hTwo->item(0)->nodeValue where item(0) has the index of the item I'll get the nodeValue .

If there is more than one <h2> item on the page $hTwo->length will return the total of items with the same TagName on the page, then you can make a loop to print all these elements

Example:

<?php 
$html = "<p>Olá!</p>
    <h1>Titulo H1</h1>
    <h2>Titulo H2</h2>
    <h2>Titulo H2 2</h2>";
// Cria um novo objeto DOM
$dom = new domDocument('1.0', 'utf-8');
// Carrega o HTML no Objeto
$dom->loadHTML($html);
//remove espaços em branco
$dom->preserveWhiteSpace = false;
$hTwo = $dom->getElementsByTagName('h2'); // Defina aqui a sua tag desejada
for ($i = 0; $i < $hTwo->length; $i++) {
    echo $hTwo->item($i)->nodeValue."<br>";
}
//Irá retornar "Titulo H2<br>Titulo H2 2;
?>
    
31.08.2017 / 16:05