How to copy a piece of text within a textarea

Asked

Viewed 212 times

0

I have a text written in <TextArea> on a page html and liked in php read the text and remove a piece of it and write in another <TextArea>, tried to use the preg_match but I couldn’t quite understand how it works, 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
ALL?

I need to read whatever’s 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
  • https://answall.com/questions/163557/pegar-o-texto-entre-uma-tag/163561#163561

  • @Magichat already tested the answer was blank

  • As you have tested?

  • @Magichat does not work because if you have more tags will show all.

2 answers

1


preg_match and preg_match_all use REGEX and return a Boolean indicating whether the (default) expression has been found. About Regular Expressions you can ask your questions here and test here

As for the problem itself, you can do so:

<?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 catch more than one occurrence, use the preg_match_all, thus:

<?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 that is inside / is the default. what is outside are modifiers. The modifier i indicates to ignore the case (uppercase and minuscule) 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 also that in <\/h1> the slider has been escaped for REGEX not to be confused with the closing slider.

You can see more about the modifiers here.

  • The code works like this, but let’s assume that instead of 1 <H1> I have 2.. I’ve tried to remove '(.)' and '.' but it didn’t work.

  • @Tmc modified the answer to answer your question

  • @Tmc plus the most complete answer, about q vc said he removed the parentheses: don’t do it. Parentheses indicate group. It’s their content that will be saved within $match. If you don’t have the parentheses, nothing gets picked up.

-1

Don’t do parse of 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 you notice I’m printing $hTwo->item(0)->nodeValue where item(0) owns the item index I will pick up the nodeValue.

If there is more than one item <h2> 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;
?>
  • the code so only works but let’s assume that instead of 1 <H2> I have 2 in the code the way to show both? I forgot to add that to the question

  • Edited with the answer to your question

  • Even though Hava only one this code will work.

  • Your example is working, +/- because I need to get the text from Textarea, but when I use $html = $_POST['htmlcode']; to get the text gives me error. PS: Doing as in the example is working

  • @Tmc This is probably because what is coming from the textarea does not have a valid HTML code. For your case, however, I believe that the use of preg_match_all will be sufficient, as indicated here: https://answall.com/questions/233752/como-copiar-um-peda%C3%A7o-text-within-a-textarea/233766#233766

Browser other questions tagged

You are not signed in. Login or sign up in order to post.