cssnb2329
cssnb2329

Reputation: 41

What mean [^>] in regex preg php?

Example code:

<?php
$html = <<< html
<p><a href="http://www.google.com" title="10">google.com</a></p>
<p><a href="http://www.cade.com" title="11">cade.com</a></p>
html;

echo preg_replace('#<p><a href\="([^>]+)" title="([^>]+)">([^>]+)</a></p>#','<p>$1 - $2</p>',$html);
?>

It works fine but i would like to know what [^>] means. I know that

But I don't know about ^>

Upvotes: 3

Views: 273

Answers (6)

itorres
itorres

Reputation: 360

You have it in the PHP documentation for PCRE regex syntax

First, you have a list of meta-characters. You can check there to find the meaning of characters in regexes.

Concerning your question we find that:

  • The [ sign starts a character class definition, finished by ].
  • The use of ^ as the first character of a character class negates the character class.
  • > isn't a meta-character.

So, [^>] is any character that isn't >

Upvotes: 0

K-ballo
K-ballo

Reputation: 81379

[^>] means a set of characters including all characters but >.

Upvotes: 1

Mark Byers
Mark Byers

Reputation: 838796

It means any character apart from >.

(Side note: it is not usually a good idea to use regex to parse HTML.)

Upvotes: 1

user142162
user142162

Reputation:

^, when placed at the start of a character class ([) means any character EXCEPT what's in the class.

In your code, that would mean it would match any character except >.

Upvotes: 2

JohnD
JohnD

Reputation: 4002

It means any character other than >

Upvotes: 3

Darin Dimitrov
Darin Dimitrov

Reputation: 1039228

It means that it should match any other character than >. It also means that the person that wrote this code and tried to parse HTML with regex didn't read the Bible and will very soon regret it.

Upvotes: 1

Related Questions