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
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))

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.