Hi to all,
I would like to get field values from a multi-line text.
For instance :
"
Name: Mr Smith
Address: Paris
"
I've tried to use (I'm a total regexp newbie, so I took it from the doc):
eregi("Name: (.*)\n",$Variab leHoldingTheTex t,$parts);
Problem: $parts[1] starts at the right place, but does not stop at the
end of the line. I get :
"Mr Smith
Address: Paris
"
Where am I wrong ? I've tried to google around, but didn't find anything
that could help me...
Thanks in advance for your help/hints/links !
BR
--
Damien 5 4157
Damien wrote: Hi to all, I would like to get field values from a multi-line text.
For instance : " Name: Mr Smith Address: Paris "
Im not sure what you want, but if you want to get the different lines in
different variables, then you can use the explode()-function to split
the string:
$lines = explode("\n", $string);
The above code would give an array $lines where each array-element
contains one line of $string.
If $string contains
"Name: Mr Smith
Address: Paris"
then $lines[0] contains "Name: Mr Smith" and $lines[1] contains
"Address: Paris".
More information on the explode()-function: www.php.net/explode
Zilla
Zilla a écrit :
(snip) Im not sure what you want, but if you want to get the different lines in different variables, then you can use the explode()-function to split the string:
(snip)
Thanks for this. I have several values to look for, and I don't know in
what lines they are (imagine a form lost in the middle of a text). This
is why I did not break up the text in lines. I'd rather not look for
evey value in every line...
Any other idea ?
BR,
Damien
Damien wrote:
.... Thanks for this. I have several values to look for, and I don't know in what lines they are (imagine a form lost in the middle of a text). This is why I did not break up the text in lines. I'd rather not look for evey value in every line...
I think I found out whats wrong. I think I have read some place that the
function looks for the largest possible pattern. So if you are looking
for some a letter followed by a : in the following:
"x:y:z"
it will return "x:y:" and not "x:" as you were looking for.
So in your example there must be a newline after "Adress: Paris" so it
takes this line too.
When that is said, i can't figure out just now, how to prevent it from
doing that. But I'll think about it and get back later if i figure it out.
Zilla
I would recommend using the Perl compatible regexp functions over the
POSIX ones. They're faster and more powerful. People also tend to be
more familiar with the Perl syntax.
The following snippet does what you want:
<?
$s = "
Name: Mr Smith
Address: Paris
";
preg_match_all( '/^(.+?): (.+?)$/m', $s, $m, PREG_SET_ORDER) ;
foreach($m as $set) {
echo "{$set[1]} = {$set[2]}<br>";
}
?>
The m modifier at the end tells the regexp engine to treat newline as
end of line. The ^ assertion, which normally would only match the
beginning of the string, now match sees a match immediately following
at newline. The same for the $ end-of-line assertion.
The (.+?) parts mean capturing at least one character, with greediness
turned off. Greediness dictates whether the regexp engine will try to
find the longest possible match. We don't want it to be greedy here.
Given the string "Time: 8:00PM", for example, we want the engine to
match up the first colon and stop, instead of trying to find another
colon that'd yield a longer match.
In an actual programming situation I would use the following pattern to
deal with possible errant white spaces:
/^\s*(.+?)\s*:\s *(.+?)\s*$/m
Chung Leong a écrit : I would recommend using the Perl compatible regexp functions over the POSIX ones. They're faster and more powerful. People also tend to be more familiar with the Perl syntax.
Hi,
Thanks for this tip... I had gone for the first one in the PHP doc ;) The following snippet does what you want:
(snip)
Thanks for the code and espescially for the time you took explaining it!
This will make me progress quite faster now ;)
I'll try you suggestion tomorrow first thing. I'll be glad to get rid of
all those substr and stristr...
Thanks again !
BR,
Damien This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Jesper Stocholm |
last post by:
I need to be able to detect URIs in some text and after this replace
dem with HTML-anchors, that is
http://www.tempuri.org/page.html
should be replaced with
<a href="http://www.tempuri.org/page.html">http://www.tempuri.org/page.html</a>
I have made the following code:
|
by: RobG |
last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE)
and have a general function (slightly modified from one originally
posted by Steve van Dongen) for getting style properties:
function GetCurrentStyle( el, prop ) {
if ( window.getComputedStyle ) {
// Mozilla et al
return window.getComputedStyle(el, '').getPropertyValue(prop) );
} // IE5+
else if ( el.currentStyle ) {
|
by: Nick |
last post by:
Hi,
How can I check if a number exists by itself in this string by using
the RegExp object?
---
var mystring = "11,111,01,011";
var match = "1";
var re = new RegExp( match );
|
by: Jon Maz |
last post by:
Hi All,
I want to strip the accents off characters in a string so that, for example,
the (Spanish) word "práctico" comes out as "practico" - but ignoring case,
so that "PRÁCTICO" comes out as "PRACTICO".
What's the best way to do this?
TIA,
|
by: Matt Kruse |
last post by:
Are there any current browsers that have Javascript support, but not RegExp
support?
For example, cell phone browsers, blackberrys, or other "minimal" browsers?
I know that someone using Netscape 3 would fall into this category, for
example, but that's not a realistic situation anymore.
And if such a condition exists, then how do you guys handle validation using
regular expressions, if the browser lacks them?
For example:
| |
by: shellon |
last post by:
Hi all:
I want to match a string that not ended with the string
"welcome", how do write the RegExp? Appreciate your help!
|
by: problematic |
last post by:
Hello guys
Actually i dont know does mysql support prepare with regexp but i have this query.
prepare stm from "select count(id) as total from products where content regexp ']?]' ";
set @a = "keyword";
execute stm using @a;
it gives error i think it is about usage ? in regexp condition but i dont know how to use ? in regexp.
thanks
i am using MySQL 5.0.18 on windows xp+sp2
|
by: reflex |
last post by:
Hi,
i have script with pattern (href=?(.*)({1}+)?),
where i match any occurence of url and replace hyperlink so i have
only anchor in it.
var regExp = /href=?(.*)({1}+)?/ig;
var wholeContent = document.body.innerHTML; //doenst look like this
I only make it for example
wholeContent = wholeContent.replace(regExp, 'href="$2"');
|
by: Nathan Sokalski |
last post by:
I have the following script that I am using to test some JavaScript RegExp
code:
function RE()
{
var testing1=new RegExp("*");
var testing2=new RegExp("{0,}");
var testing3=new RegExp("+");
var testing4=new RegExp("{1,}");
window.alert(testing1.test("ab")+"\n"+testing2.test("ab")+"\n"+testing3.test("ab")+"\n"+testing4.test("ab"));}When this script is run, the window.alert contains the following results:truetruefalsefalseThe the first...
|
by: pjacobi.de |
last post by:
Dear All,
I'm looking for a function which, given a regexp re and and a string
str, returns
whether re won't match any string starting with str. (so it would
always
return False if str is "" or if str itself matches re -- but that are
only the
easy cases).
|
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,...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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...
|
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();...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |