Hey all,
I have some data like mg-st-005.xyz.pqr.dyd .
I want to have the string mg-st-005 ( string before the first "."(dot)).
My preg_match does not work properly....
can you please help me?
Thanks...
12 1466
what is 'preg_match' ie what is the code and its purpose?
are you just after code that will retrieve the string information before the first '.'?
what is 'preg_match' ie what is the code and its purpose?
are you just after code that will retrieve the string information before the first '.'?
hey leeogrady,
yes, I just wanted to have the string before the first ".".
And for that I used preg match..
My code was, -
preg_match("/(.*)$./",$hostname,$matches);
-
where host name is the mg-st-005.xyz.pqr.dyd
Thanks...
I am still not sure what preg_match is, but i think there is a better way...
I would use strtok() a built in function to PHP
the code is something like: -
$delims = "/(.*)$/";
-
$hostname = "mg-st-005.xyz.pqr.dyd";
-
-
$word = strtok($hostname, $delims);
-
if($word == "mg-st-005") {
-
'IT IS A MATCH!
-
}
-
I hope this helps
Lee
I am still not sure what preg_match is, but i think there is a better way...
I would use strtok() a built in function to PHP
the code is something like: -
$delims = "/(.*)$/";
-
$hostname = "mg-st-005.xyz.pqr.dyd";
-
-
$word = strtok($hostname, $delims);
-
if($word == "mg-st-005") {
-
'IT IS A MATCH!
-
}
-
I hope this helps
Lee
Would explode() not work?
[php]
$hostname = explode('.', 'mg-st-005.xyz.pqr.dyd');
echo $hostname[0];
[/php]
I am still not sure what preg_match is, but i think there is a better way...
You've never used any of the preg_* functions? You use them in conjunction with regular expressions. preg_match(), preg_replace(), etc.
No i havent!?! thanks for that markusn00b, i will look in to them!
Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!
No i havent!?! thanks for that markusn00b, i will look in to them!
Dam. Yes of course, explode() is what i was trying to think of but couldnt! strtok() was the only thing i could think of!
When you get your head around regular expressions (I still haven't), you'll be able to be alot more effective in your programming.
Darn that explode()!
Using explode() is the most efficient. But here's how to do that with preg_match: -
<?php
-
$hostname = 'mg-st-005.xyz.pqr.dyd';
-
-
preg_match("/^([^\.]+)\..+$/", $hostname, $matches);
-
-
echo $matches[1]; /* will print: mg-st-005 */
-
?>
-
Thanks all,
The problem is solved..
Thanks for the help..
Thanks all,
The problem is solved..
Thanks for the help..
Glad we could all be of assistance!
I know I'm coming in here a little late, but... -
$custom = substr($hostname, 0, strpos($hostname, '.'));
-
I know I'm coming in here a little late, but... -
$custom = substr($hostname, 0, strpos($hostname, '.'));
-
Hey Pbmod,
Thanks for the solution..
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Kenneth McDonald |
last post by:
I'm working on the 0.8 release of my 'rex' module, and would appreciate
feedback, suggestions, and criticism as I work towards finalizing the
API and feature sets. rex is a module intended to make...
|
by: Bradley Plett |
last post by:
I'm hopeless at regular expressions (I just don't use them often
enough to gain/maintain knowledge), but I need one now and am looking
for help. I need to parse through a document to find a URL,...
|
by: Dimitris Georgakopuolos |
last post by:
Hello,
I have a text file that I load up to a string. The text includes
certain expression like {firstName} or {userName} that I want to match
and then replace with a new expression. However,...
|
by: Joe |
last post by:
Hi,
I have been using a regular expression that I don’t uite understand to
filter the valid email address. My regular expression is as follows:
<asp:RegularExpressionValidator...
|
by: Billa |
last post by:
Hi,
I am replaceing a big string using different regular expressions (see
some example at the end of the message). The problem is whenever I
apply a "replace" it makes a new copy of string and I...
|
by: Zach |
last post by:
Hello,
Please forgive if this is not the most appropriate newsgroup for this
question. Unfortunately I didn't find a newsgroup specific to regular
expressions.
I have the following regular...
|
by: Mike |
last post by:
I have a regular expression (^(.+)(?=\s*).*\1 ) that results in
matches. I would like to get what the actual regular expression is.
In other words, when I apply ^(.+)(?=\s*).*\1 to " HEART...
|
by: shawnmkramer |
last post by:
Anyone every heard of the Regex.IsMatch and Regex.Match methods just
hanging and eventually getting a message "Requested Service not
found"?
I have the following pattern:
^(?<OrgCity>(+)+),...
|
by: NvrBst |
last post by:
I want to use the .replace() method with the regular expression /^ %VAR
% =,($|&)/. The following DOESN'T replace the "^default.aspx=,($|&)"
regular expression with "":...
|
by: Andy B |
last post by:
I need to create a regular expression that will match a 5 digit number, a
space and then anything up to but not including the next closing html tag.
Here is an example:
<startTag>55555 any...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |