473,387 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

preg_match/replace to fix a search query

I am working on a script to handle a search query. In some instances,
the query could come through as "isbn:%20#############" (where %20 is
an encoded space and the colon is optional). Basically I want to
strip off the ISBN portion and leave just the numbers if that is the
case.

Orignally I was trying
$value = $_GET["query"];
if (preg_match("isbn:?%20", $value))
{
$isbn = preg_replace("isbn:?%20", "", $value)
}
else
{
$isbn = $value
}

Doesn't seem to work though. Any thoughts?

Mar 16 '07 #1
3 3348
On Mar 16, 5:19 pm, "fie...@gmail.com" <fie...@gmail.comwrote:
I am working on a script to handle a search query. In some instances,
the query could come through as "isbn:%20#############" (where %20 is
an encoded space and the colon is optional). Basically I want to
strip off the ISBN portion and leave just the numbers if that is the
case.

Orignally I was trying
$value = $_GET["query"];
if (preg_match("isbn:?%20", $value))
{
$isbn = preg_replace("isbn:?%20", "", $value)}

else
{
$isbn = $value

}

Doesn't seem to work though. Any thoughts?
I wouldn't use regex unless really nessessary.
I assume your problem lies with the space character. Usually, the
script transforms encoded url characters back into their original
form. I suggest using a normal space char in the script, and just to
make sure, a str_replace("%20", " ", $string) in the beginning.

Mar 16 '07 #2
fi****@gmail.com wrote:
if (preg_match("isbn:?%20", $value))
...
Doesn't seem to work though. Any thoughts?
- The pattern of preg_match needs delimiters. Mostly slashes are used
for this: /pattern/
- Each statement should be ended with a semicolon;
- Note that PHP automatically URL-decodes the GET parameters, so %20 in
the URL will become a space in the GET variable.
- You can use the matches parameter to retrieve the ISBN number.
- When asking a question here, "doesn't seem to work" is not good
enough. Insert echo statements to see whether the preg_match actually
matches and report any error you get in your post.

if (preg_match("/isbn:? ([0-9X]*)/", $value, $match))
{
$isbn = $match[1];
}
else
{
$isbn = $value;
}

Sjoerd
Mar 16 '07 #3
Thanks for all the input. My solution ended up somewhat different,
but in the same vein.

Another script handed off the data based on a regex trigger, therefore
I knew if this was invoked, it had to be a valid ISBN number, so no
need to actually test if it matched since I know it does. If the
query started with "ISBN" or "ISBN:", regardless of the number the
total length would be 13, which is the longest the input could be
and be a valid ISBN, so I used:

if (strlen($bookISBN) 13)
{
$bookISBN = preg_replace('/(\w+)(:)? (\d=)/', '$3', $bookISBN);
}

So far, so good. Everything works just as it should now.

On Mar 16, 1:15 pm, Sjoerd <sjoerd-n...@linuxonly.nlwrote:
fie...@gmail.com wrote:
if (preg_match("isbn:?%20", $value))
...
Doesn't seem to work though. Any thoughts?

- The pattern of preg_match needs delimiters. Mostly slashes are used
for this: /pattern/
- Each statement should be ended with a semicolon;
- Note that PHP automatically URL-decodes the GET parameters, so %20 in
the URL will become a space in the GET variable.
- You can use the matches parameter to retrieve the ISBN number.
- When asking a question here, "doesn't seem to work" is not good
enough. Insert echo statements to see whether the preg_match actually
matches and report any error you get in your post.

if (preg_match("/isbn:? ([0-9X]*)/", $value, $match))
{
$isbn = $match[1];}

else
{
$isbn = $value;

}

Sjoerd

Mar 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

22
by: Justin Koivisto | last post by:
OK, I found a thread that help out from a while back (Oct 9, 2002) to give me this pattern: `(((f|ht)tp://)((+)(+)?@)?()+(:\d+)?(\/+)?)`i OK, all is well and good with this until the URL is...
2
by: Muumac | last post by:
I have problem with large textfiles! When I load over 4MB xml and then try to preg_match something in this I get always FALSE! I have <File>....</File> tags in XML. Between tags is files contents...
3
by: ben | last post by:
Hi, I've been tryin to use preg_match to search through a string. But it doesn't seem to function with a variable as the search string. I've tried many variations, but I'm not sure how to do it....
22
by: stoppal | last post by:
need to extract all text between the following strings, but not include the strings. "<!-- #BeginEditable "Title name" -->" "<p align="center">#### </p>" I am using preg_match(????, $s,...
4
by: siromega | last post by:
I have a string I'd like to have broken into parts using preg_match. I used a regular expression from the Perl FAQ (http://perlfaq.cpan.org/): push(@new, $+) while $text =~ m{ "(*(?:\\.*)*)",?...
5
by: Mark Woodward | last post by:
Hi all, I'm trying to validate text in a HTML input field. How do I *allow* a single quote? // catch any nasty characters (eg !@#$%^&*()/\) $match = '/^+$/'; $valid_srch = preg_match($match,...
0
by: xmanofsteel69 | last post by:
I'm trying to create a search function for my site and I can't ever seem to figure it out. If anybody could help, that would be awesome, because everything I try, I keep getting errors... Here's...
2
by: JanDoggen | last post by:
function vldLicense($lic) { echo "called with lic: ". $lic . "<br>"; echo preg_match('', $lic) . "<br>"; if (preg_match('{4}-{4}-{4}-{4}', $lic) == 0) return false; return true; } gives me:
6
by: simon.robin.jackson | last post by:
Ok. I need to develop a macro/vba code to do the following. There are at least 300 corrections and its expected for this to happen a lot more in the future. Therefore id like a nice...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.