Jump to content

[SOLVED] preg_match regex


kid85

Recommended Posts

I use this link to add products to my cart:

 

<a href="../folder/file.php?action=addginseng01<?php // ,product name - per box,25.95?>

 

1.

How can I write something which returns the word that is between "add" and "01" which is "ginseng".)

 

2.

Secondly, I need to extract the middle black part of this string as well. I tried using preg_match but I can't get it to work.

 

addginseng01<?php // ,product name - per box,25.95?>

preg_match('/('.$_GET["action"].'<\?php[ \t]\/\/[ \t])(\?>)/i', $filecontents, $target)

 

Can anybody help ?

Link to comment
https://forums.phpfreaks.com/topic/83373-solved-preg_match-regex/
Share on other sites

like this

<?php
//if $_GET["action"] = addginseng01 then $result will = ginseng
$result = "";
if (preg_match('/add([^\d]*)/i', $_GET["action"], $result))
{
$result = $result[1]; // = ginseng
}

echo $result;
?>

 

EDIT:

You could also use, IF the word may contain numbers

if (preg_match('/add(.*?)\d{2,2}/i', $_GET["action"], $result))

Your code was perfect. I can't figure the last part :

 

This is the code to search for and return the middle black part

addginseng01<?php // ,product name - per box,25.95?>

 

I tried this but seems light years away.

 

$target = "";
if (preg_match('/('.$_GET["action"].'<\?php[ \t]\/\/[ \t])(\?>)/i', $filecontents, $target))
{
$target = $target[1];
}
print $target;

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.