Jump to content

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;

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.