D
D
dura2282018-02-22 19:12:28
PHP
dura228, 2018-02-22 19:12:28

What's wrong with the regex for h1 tag search?

$text="fhdsklf sdoijf ds <h1>jfidsfj</h1> jklsfdjflks";
$pattern ="!<h1>.+</h1>!";
preg_match($text, $pattern, $matches);
print_r($matches);

The code throws an error: "Warning: preg_match(): Delimiter must not be alphanumeric or backslash"

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rodion, 2018-02-22
@dura228

You need to swap $pattern and $text
Also I advise you to use .+? instead of .+, because otherwise, the regular expression will look for the last occurrence in the string.

$text="fhdsklf sdoijf ds <h1>jfidsfj</h1> jklsfdjflks";
$pattern ="!<h1>.+?</h1>!";
preg_match($pattern, $text, $matches);
print_r($matches);

D
DarkByte2015, 2018-02-22
@DarkByte2015

Regulars and html parsing. Never do that. You have to use special. libraries, for example, here you can choose something. In general, it is better not to use regular games anywhere and never. They say using regular expressions you get two problems from one at once.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question