Expression régulière simple mais je bloque

WRInaute passionné
Bonjour à tous,

Pour faire simple, je dois trouver le dernier chiffre d'une série de chiffre séparés par un underscore :
$data = 52_103;

J'ai fait ceci :
Code:
preg_match("([0-9]{1,3})[^_]", $data, $matchs);

Mais ça ne fonctionne pas, quelqu'un peut m'aider ?

Il faut que ça fonctionne aussi si $data = 52 par exemple, donc pas de underscore dans $data
 
WRInaute accro
Pourquoi utiliser un regex pour ça ?
PHP:
<span class="syntaxhtml"><br /><span class="syntaxdefault"><?php&nbsp;<br />$data&nbsp;</span><span class="syntaxkeyword">=&nbsp;</span><span class="syntaxstring">'52_42_80_103'</span><span class="syntaxkeyword">;<br />echo&nbsp;</span><span class="syntaxdefault">array_pop</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">explode</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'_'</span><span class="syntaxkeyword">,&nbsp;</span><span class="syntaxdefault">$data</span><span class="syntaxkeyword">));<br /></span><span class="syntaxdefault">?><br /></span></span>
http://codepad.org/jon6Kj1L
 
WRInaute passionné
Merci zeb pour le lien, j'ai refait un topic en expliquant plus "simplement"

@spout : merci pour ton code, je vais tester et je vous fait un retour. C'est sûr que ça prendra le dernier chiffre hein ?
Dsl d'insister mais les expressions régulières c'est vraiment pas mon truc !
 
WRInaute passionné
Merci Spout, ça marche à la perfection, bonne idée le explode, pourquoi n'y ai-je pas pensé ?! A chaque fois tu me réponds en 2 lignes et ça marche toujours parfaitement, j'adore ! Dès que je suis millionnaire je t'embauche lol

Voici le code final qui traite mes URLs de catégories si ça peut aider quelqu'un qui passerais par là :
PHP:
<span class="syntaxdefault"></span><span class="syntaxcomment">//on redirige si l'URL courante est différente de l'URL SEO<br /></span><span class="syntaxdefault">if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(isset(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'cPath'</span><span class="syntaxkeyword">]))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(!isset(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">||</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">==</span><span class="syntaxdefault"> </span><span class="syntaxstring">'1'</span><span class="syntaxkeyword">))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          $request_category_id </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array_pop</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">explode</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'_'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'cPath'</span><span class="syntaxkeyword">]));<br /></span><span class="syntaxdefault">          $seo_url </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> tep_href_link</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">FILENAME_DEFAULT</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"cPath="</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$request_category_id</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">          $current_url </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"http://"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'HTTP_HOST'</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'REQUEST_URI'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">              if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$current_url </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> $seo_url</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">              header</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Status: 301 Moved Permanently"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 301</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">              header</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Location: "</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$seo_url</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">              </span><span class="syntaxkeyword">}<br />}</span><span class="syntaxdefault"> elseif </span><span class="syntaxkeyword">(</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(isset(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'cPath'</span><span class="syntaxkeyword">]))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(isset(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">])</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">&&</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">]</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'1'</span><span class="syntaxkeyword">))</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">          $request_category_id </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> array_pop</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">explode</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'_'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'cPath'</span><span class="syntaxkeyword">]));<br /></span><span class="syntaxdefault">          $seo_url </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> tep_href_link</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">FILENAME_DEFAULT</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">"cPath="</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$request_category_id</span><span class="syntaxkeyword">).</span><span class="syntaxstring">"?page="</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$_GET</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'page'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">          $current_url </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">"http://"</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'HTTP_HOST'</span><span class="syntaxkeyword">].</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'REQUEST_URI'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">              if </span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$current_url </span><span class="syntaxkeyword">!=</span><span class="syntaxdefault"> $seo_url</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">              header</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Status: 301 Moved Permanently"</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> false</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> 301</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">              header</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"Location: "</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$seo_url</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">              </span><span class="syntaxkeyword">}</span><span class="syntaxdefault"> </span>
Edit: Comment fais-on pour coloriser le code PHP comme le message de Spout ?
 
Discussions similaires
Haut