473,811 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

preg_match issue

Hello,

I am trying to split a string that contains parentheses; the aim here is
to keep the part that's before the opening parenthese:

the string (quoted here) is "36 (72 cm)"
First I want to know if the string contains at least an openning
parenthese; if it does, I split it and extract the first token (ie "36 ").
My code follows

if (preg_match("/\(/", $sPermutationNa me)) {
$tokens = explode("\(", $sPermutationNa me);
$sPermutationNa me = $tokens[0];
}

but that does not work. Would anyone care to enlighten me on this one?
Sep 24 '07 #1
3 1744
On 24 Sep, 13:52, Henri <h...@dontbothe r.fuwrote:
Hello,

I am trying to split a string that contains parentheses; the aim here is
to keep the part that's before the opening parenthese:

the string (quoted here) is "36 (72 cm)"
First I want to know if the string contains at least an openning
parenthese; if it does, I split it and extract the first token (ie "36 ").
My code follows

if (preg_match("/\(/", $sPermutationNa me)) {
$tokens = explode("\(", $sPermutationNa me);
$sPermutationNa me = $tokens[0];

}

but that does not work. Would anyone care to enlighten me on this one?
"does not work", ahh how helpful! That tells us exactly what you are
actually seeing - NOT!!!

As a matter of interest, why are you exploding on "\(", when \ doesn't
appear in the target string?

Sep 24 '07 #2
Henri wrote:
Hello,

I am trying to split a string that contains parentheses; the aim here is
to keep the part that's before the opening parenthese:

the string (quoted here) is "36 (72 cm)"
First I want to know if the string contains at least an openning
parenthese; if it does, I split it and extract the first token (ie "36 ").
My code follows

if (preg_match("/\(/", $sPermutationNa me)) {
$tokens = explode("\(", $sPermutationNa me);
$sPermutationNa me = $tokens[0];
}

but that does not work. Would anyone care to enlighten me on this one?
Hi Henri

regular expressions would be on overkill here.

strtok($a, "(")

does exactly what you want - returns a portion of the string before "(".
If there's no "(" , the whole string will be returned.


--
gosha bine

extended php parser ~ http://code.google.com/p/pihipi
blok ~ http://www.tagarga.com/blok
Sep 24 '07 #3
On Mon, 24 Sep 2007 06:31:28 -0700, Captain Paralytic wrote:
On 24 Sep, 13:52, Henri <h...@dontbothe r.fuwrote:
>Hello,

I am trying to split a string that contains parentheses; the aim here
is to keep the part that's before the opening parenthese:

the string (quoted here) is "36 (72 cm)" First I want to know if the
string contains at least an openning parenthese; if it does, I split it
and extract the first token (ie "36 "). My code follows

if (preg_match("/\(/", $sPermutationNa me)) {
$tokens = explode("\(", $sPermutationNa me); $sPermutationNa me =
$tokens[0];

}

but that does not work. Would anyone care to enlighten me on this one?

"does not work", ahh how helpful! That tells us exactly what you are
actually seeing - NOT!!!
mmh... forgive me. What I clumsily tried to mean is that the preg_match
did not seem to see any parenthese in the string and therefore did not
execute the two following statements.
As a matter of interest, why are you exploding on "\(", when \ doesn't
appear in the target string?
because I thought that escaping the parenthese would avoid a compilation
error due to the code's being misinterpreted (ie: reporting a parenthese
mismatch).
Anyway, I think I have a solution now.
Sep 24 '07 #4

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

Similar topics

2
4249
by: fartsniff | last post by:
hello all, here is a preg_match routine that i am using. basically, $image is set in some code above, and it can be either st-1.gif or sb-1.gif (actually it randomly picks them from about 100 gifs). then it processes them based off of which image type it selected, either the st- 's or the sb- 's.
2
4698
by: Han | last post by:
I'm wondering if someone can explain why the following works with preg_match_all, but not preg_match: $html = "product=3456789&amp;" preg_match_all ("|product=(\d{5,10})&amp;|i", $html, $out); $out = 3456789 preg_match ("|product=(\d{5,10})&amp;|i", $html, $out);
5
7567
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, $res_description); if (!$valid_srch) { ...
6
10254
by: mantrid | last post by:
Hello Found this piece of code using preg_match to check file types during upload of files. $allowed_file_types = "(jpg|jpeg|gif|bmp|png)"; preg_match("/\." . $allowed_file_types . "$/i", $_FILES) I understand the basic preg_match but am confused as to how the string pattern part is working i.e. "/\." . $allowed_file_types . "$/i"
4
1923
by: cainwebdesign | last post by:
I need to create a simple page to find the .gif file below from the page below. No matter what I try it doesn't work.... Any ideas? http://www.toysrus.com/product/index.jsp?productId=2327085 prod_AddtoCart.gif
6
2642
by: jsamdirect | last post by:
I am having a weird issue with php preg_match. I am moving to my php sites to a new server. On the old server all works well. On the new server preg_match always returns false. If I log in via ssh and run the script at the bash prompt as root preg_match works fine. Does anyone have any thoughts on what would cause this issue?
2
4257
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:
13
5379
by: chadsspameateremail | last post by:
I might have found a problem with how preg_match works though I'm not sure. Lets say you have a regular expression that you want to match a string of numbers. You might write the code like this: preg_match( '/^+$/', $TestString ); OK everything seems fine. However, did you know if you pass the following to preg_match: "12345\n" it will return that a match occurred?!? Even though the newline is not a valid character in our regular...
8
4002
by: Thomas Mlynarczyk | last post by:
Hello, I want to split a given string into tokens which are defined by regexes: // example tokens - a bit more complex in real $tokens = array( 'NUMBER' ='~^\d+~', 'NAME' ='~^+~', 'ANY' ='~^.~' ); // make sure there is always a match
0
9605
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10130
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9204
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7667
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6887
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5553
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.