3

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

  • + = 1 or more;
  • () = subpatterns;

But I don't know about ^>

JohnD
  • 3,884
  • 1
  • 28
  • 40
cssnb2329
  • 41
  • 1
  • Please mark an answer that has helped you as accepted, you have not done this for any of your questions. – JohnD Sep 18 '11 at 19:40

6 Answers6

3

It means any character other than >

JohnD
  • 3,884
  • 1
  • 28
  • 40
2

^, 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 >.

  • it will match all characters (.+) isn't? – cssnb2329 Sep 17 '11 at 21:42
  • No, . matches any character. [^] matches any character but the characters following the ^ before the ]. [^aiu] would match any character except a or i or u [^a-f] would match any character except a, b, c, d, e, f [^.] would match any character except . – itorres Sep 17 '11 at 21:51
1

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
1

It means any character apart from >.

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

Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
  • Agreed: This is because HTML is not a regular language ( http://stackoverflow.com/questions/590747/using-regular-expressions-to-parse-html-why-not ). For small limited input it can be OK but for large complex non-standard data it will break and leave you crying. +1 – Gunnar Hoffman Sep 17 '11 at 21:43
1

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

K-ballo
  • 80,396
  • 20
  • 159
  • 169
0

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 >

itorres
  • 359
  • 3
  • 10