Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




How Do I Get This Value From A Regular Expression


I'm having trouble with preg_match. I want to get the value in
parenthese from this pattern

$pattern = "/ab(.*)cde/";
$str = "abxyzcde";

What preg_match expression would i have to write to get the value "xyz"
into a string?

I'm using php 4.4.4.




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
Regular Expression
I'm trying to replace all $ with $$ in a string. I'm using the following dead simple expression ereg_replace ("$", "$$"). The fact is that this also changes the end of string mark of a PHP variable. When I'm trying to compensate this with the expression.
ereg_replace ("$[^$]", "$$"). The sematics are every $ execept the $ sign at the end of the variable. This doesn't work. Does anyone have the correct expression or an alternative to solve the problem with one line of code?

Need Help With A Regular Expression
I need to replace all quotes with an arbitrary character - but only if they are between tags:

<span class="large">big text</span>

This would become something like:

<span class=QlargeQ>big text</span>

I've been trying lots of permutations of this, without success:

$txt = preg_replace("/<(.*?)("){1,}(.*?)>/", "<1Q3>", $txt);

Regular Expression
I've got a tag(let's call it [tag]) that has several <br>s in front of it. I want to get rid of the <br>s.

The problem here is that there are an unknown amount of <br>s before the tag, and an unknown amount of spaces in between seemingly random placements of <br>s. Here is an example, to clarify.

$str = "<br> <br><br><br> <br><br> <br> <br> [tag]";

I just randomly wrote this, so this isn't real, but it's similar to my situation. The regular expression I wrote to get rid of one <br> was...

$pattern = "<br>[tag]";

then the replacement would be....

$replacement = "[tag]";

Regular Expression
Hi, I've a string like this:

$data="#title {color:red;font-size:12pt}";

I would like to use

ereg("^(.+){(.+).+);(.+)$",$data,$bit);

to delimite $data into pieces saving as $bit[0], $bit[0]...etc I want to have result is

$bit[0]=#title
$bit[1]=color
$bit[2]=red
$bit[3]=font-size
$bit[4]=12pt

Can I do this, I want to use "{", ":", ";" and "}" as delimitator to split out the string into pieces, am I using the regular expression alright?

Regular Expression
how to first take away all spaces that er more then two and replace them with ; and then take all the K which have one space on each side and change it to ; like [:space:]K[:space:] into ;

Regular Expression
preg_match("/<some_tag >([^']*?)</some_tag>/", $data, $matches)
preg_match_all("/<p>(.*?)</p>/", $matches[1], $paragraphs);

foreach ($paragraphs[0] as $paragraph) {
$content=$content.$paragraph;
}

The above code only works if <pis the first tag under <some_tag>. i.e,
works with

<some_tag >
<p>blah</p>
</some_tag>

but not with

<some_tag >
<some_other_tag>blah</some_other_tag>
<p>blah</p>
</some_tag>


How could I make the code works regardless of the position of <punder
<some_tag>?

Regular Expression
I'm trying to add a line of code to several hundred pages; I need the line to be added right after the <body> tag - however the body tags on all the pages vary.

So I need a regular expression such as starts with "<body" and ends at the first ">" - but I cannot seem to formulate the proper expression to extract all characters between those. so ereg_replace("<body(regex)>","<body1>newlineofcode",$page);

Regular Expression
I am trying to do a regular expression on some code I have i need to: PHP Code:

Regular Expression
with a regular expression that will split a string into
several parts with ',' (comma) as the separator, but NOT where the separator
is enclosed in parentheses. For example, take the string "field1,
CONCAT(field2,' ', field3) as field23, field4". I would like to be able to
split this into the following:
[0] field1
[1] CONCAT(field2,' ', field3) as field23
[2] field4

Regular Expression Q
I have an HTML file and want to convert all tags to lowercase, but
leave the attributes alone. For instance:

<SOME TAG ATTRIBUTE="Text THAT may be Upper"></SOME TAG>

Needs to be converted to:

<some tag attribute="Text THAT may be Upper"></some tag>

I can easily convert everything in the tag to lower using '<.*?>' but am
stuck getting PHP to ignore whatever is in quotes.

Regular Expression
With the next code I stop numeric chars into a submit form. Can you tell how to modify the code to stop numeric chars AND @ char ?

<?
if(eregi("^[[:digit:]]$",$Name)) {
// My code}?>

Value From A URL Using Regular Expression
We have a URL than can be any one of the following

http://mydomain.com/CA/
http://mydomain.com/123CA/
http://mydomain.com/123CA456/
http://mydomain.com/123CA456/index.htm

In need a little help with the regular expression that would extract the value after the domain name. We want $myvalue to be equal to the part of the URL just after the domain name, but not including anything after the subsequent slash"/". for example,
http://mydomain.com/123CA456/index.htm
should save as: $myvalue = 123CA456 any ideas of the regular expression we could use?

Regular Expression
i wanna make a regular expression for telephone number. the telephone number consist of country code, area code and the phone number in the format like 91-121-243980 or it may be like 91-23-23-23-23567-89 iam trying the regular expression as [[0-9]+-]+[0-9]+
but its not working?

Regular Expression
i intend to validate a string of numbers:

The conditions are: it can start with a + sign or without. but all must be
in numbers. a space is allowed

Consider the following numbers

+12345922

12345922

The above 2 numbers are valid. I've tried the following regexp:

[+0-9]$

It doenst work as intended.

helloworld1234567

goes through!

whereas

1234567helloworld doesn't!

Regular Expression In PHP
How can I get all the values which are between Name tags as follows:

<emp>
<name>N1</name>
<age>1</age>

<name>N2</name>
<age>2</age>

<name>N3</name>
<age>3</age>

<name>N4</name>
<age>4</age>

</emp>

Regular Expression
I have used some of this code from the PHP manual, but I am bloody hopeless
with regular expressions. Was hoping somebody could offer a hand.

The output of this will put the name of a form field beside name.
I want to get the following but not sure how to modify the code below.
1. Field Name (to appear beside NAME:)
2. Field Type (to appear beside TYPE:)
3. Field Value (to appear beside VALUE:)

Make sense.
It is part way there, just need some help finishing it.

$filename = "form-eg.php"; // Open file to read HTML with Form code
$fd = fopen ($filename, "rb");
$contents = fread ($fd, filesize ($filename));
preg_match_all ('/<input.*?names*=s*"?([^s>"]*)/i', $contents,
$matches); // get all input fields and attributes and values

for ($i=0; $i< count($matches[0]); $i++) {
echo "matched: ".$matches[0][$i]."<br />";
echo "NAME: ".$matches[1][$i]."<br />";
echo "TYPE: ".$matches[3][$i]."<br />";
echo "VALUE: ".$matches[4][$i]."<br />";
}

fclose ($fd);

I will also need to run another check for:

Regular Expression
I am currently using the following regular expression to validate an email address:

(!ereg("^[a-zA-Z0-9_]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+$", $email))

Problem is it will not allow say URL what do i put in to allow it to pass dots(.) in the address?

Regular Expression -
can anyone translate this into plain english?

preg_match_all("/(w+[,. ?])+/U", $text, $words);

Regular Expression
How can I write the thing below as a regular expression (preg_replace)?

$array = array("-0", "-1", "-2", "-3", "-4");
$key = str_replace($array, "", $key);

I am trying to learn regular expressions, but I find them hard and this
seems like a really simple place to start....

Regular Expression
while going through the parsing code of search engine results I came across a expression something like this "/<a href="(.*?) ">.*?</a>/i"

This code I am using to sepate the link fro the search engine results i.e I got .*? and (.*?) I mean to say one out side the bracket and one inside the bracket. What is the difference between these same expression or characters This expression I am using with preg_match_all command.

Regular Expression
what good book would you guys recommend on using regular expressions in PHP?

Regular Expression
First wanted to say that PHPBuilder seems like an amazing site to me so far. Until a couple weeks ago I had no idea what PHP was. I recently bought a book and have already learned a decent amount, and am excited by how much PHP has to offer. Anyway, here's my question:

I want to get a page's HTML into a string, and then parse the string and return the first expression in standard US dollar form... $(one or more digits).(2 digits)

How do I go about doing this with, let's say, this page? That is just an example, it doesn't have to be Amazon.

Regular Expression
how can I check if $var contains only "----" (minus) no metter how many?

Regular Expression [Help]
I must find a regular expression to find every declaration af a
variable in a php programm.....

I tried:

/$([a-zA-0-9_])*(s|S)=(s|S)/is

But it doesn't reall go :-(

I've got no expreience in things like regular expressions...

And: Is it possible to look if the decaration ist in ' or " ? If eyes:
how and if no :-@

Regular Expression
how can i take a string and delete everything up to and including the
first occurrence of a forward slash (/)?

Regular Expression Help
I am using the preg_match function (in PHP) that uses perl regular
expressions. Apparently I don't really understand regular expressions
though.

If this is the regular expression

/^s*(d+.d+)|(.d+)|(d+)s*$/

How does this:

40:26:46.302N 79:56:55.903W

match? I thought when I added the ^ and $ that meant it had to match
the whole thing? It seems to only be matching .302

Regular Expression
I have put in a check for characters/ numbers and punctions as told by many source, but for some reason it it not working as espected and I'm banging my head on the PC now. 

if(!ereg("^[0-9A-Za-z'" ,.!;:£?$&+=/-@#%]{1,255}$",$body)){
        $bodytest++;
}

For some reason the code increments $bodytest if $body contains ` ¬ etc... but if it contains < > it does not.. but for th life of me I have not included the < > in the code to be allowed in the text.

Getting Variables Out Of A Regular Expression
If i need to do some big time parsing, and I need what I'm parsing out to go into nice little varaibles, how do I do that without a million ugly preg_split functions?

Say I have this line:

"blah blah blah ~~ [THIS] -- blah blah -- [THAT]"

How could I end up with $this and $that, containing "[THIS]" and "[THAT]" respectively,
possibly in only a few lines of code?

Regular Expression Problem
The problem: I have an html page I want to parse. before I parse it, I want to remove everything in HTML comments <!-- -->,My code:

Regular Expression Function
How do I find a pattern in a string, and simply return the text that matched the pattern?

Regular Expression (domain)
Hi, I am trying to validate a valid second level domain with this complicated stuff. Thus what I have here is : PHP Code:

Quick Regular Expression
I have a string and I need to check and see if there is an # sign in it. Is there a regular expression that will do that for me?

Regular Expression Negation
I'm trying to negate a phrase rather than just a single character in a regular expression. Is there a syntax for multiple character negation? For example, if 'Brenda Brown' and 'Brenda Jones' are OK, but not 'Brenda Smith'?
Regex is such a powerful tool -surely there's a multiple negation operator.

Basic Regular Expression
I need to check a string to make sure it only has letters, and/or numbers and/or an underscore in it.

Regular Expression Help Please (hopefully Simple)
I've got a file with this in it: [Event "Some string data"]

The data I'd like extracted is within the quotes: Some string data

I can read the file out and extract (using string positions) the data I'd
like but it would neater if I use a regular expression. Only problem is I've
never seen a working example of this type of extraction and am completely
new to PHP.

Problem With A Regular Expression
I'm having a regular expression problem. I need it to match all occurences of "href="(not http)<whatever-else"". My attemps have been:

href="[^h][^t][^t][^p][^:][^/][^/][^"]+", which works, but will fail on things like "href="attribute.html"", as there are t's where the regex doesn't expect them. Thus, this attempt can be ditched.

href="(^http://)["]+", which doesn't work, and ends up matching only things like "href="httpandthensomemore.html""

and I couldn't really figure out more. I need a regular expression that only matches the WORD http, not each individual character, and I need it to match the whole word, as this is not used as a check for links starting with http, but for replacing relative links with absolute ones in the following piece of PHP code:

preg_replace("/href="<regex that works... hopefully>"/", "http://address.com/$1", $pieceOfHTML);

And I chose to post this here and not in the PHP section, as it's not concerning the PHP but the regular expression....

Regular Expression Question
In the following regular expression:

$_="BCADeFGHIJKL";
if (/L[w]E/) { print "true"} else { print "false" };
if (/U[w]E/) { print "true"} else { print "false" };

why the first one print true while the second one print false?
Can someone explain L U?

Regular Expression Pattern
I'm having trouble with a regular expression pattern. I want to match "c" or "k" or "ck" but not "ch". It's that last bit that's giving me trouble. Example:

Match "sic"
or "sik"
or "sick"
or "sikc"
not "sich"

Here's the pattern that I'm using so far:
/(^|[^a-zA-Z])(s+|$+|&+)(W|( )|(<.*> )|s|_)*(a+|e+|i+|o+|u+|*+|&+|^+|%+|$+|#+|@+|!+)(W|( )|(<.*> )|s|_)*(c+|k+|((c+)(W|( )|(<.*> )|s|_)*(k+))|((k+)(W|( )|(<.*> )|s|_)*(c+)))([^a-zA-Z]|$)/gi

Regular Expression Problem
I am trying to extract hello world out of the following html string if
the "message" string is found in the same row.

I am not sure why it's not working.

<?php
$string = '<html>
<body>
<table>
<tr>
<td class="bg-grey-m">
<table cellpadding="12">
<tr><td>message</td><td><i>hello world</i></td></tr>
<tr><td>tel</td><td><i>111-111-555</i></td></tr>
</table>
</td>
</tr>
</table>
</body>'

preg_match_all("/<td
[^>]*class="bg-grey-m">[^<]+<table[^>]*>.*message.*<i>(.*)</i>.*</table>.*/iU",
$string, $matches);

foreach($matches[1] as $link)
{
echo "<li>$link</li>
";
}

?>

Preg_match_all // Regular Expression
right now the script just browse through the url, and returns all the links in the page, but what i would like to try, is to make it return the links AND the text between the <a href and </a>, so if a link says PHP Code:

Regular Expression Troubles
I'm not the most competent person when it comes to regular expressions. Needless to say, i attempted my own pattern and have manage to balls something up cronic The following is the pattern that i wish to convert: PHP Code:

How To Refine My Regular Expression?
What I want to do is replace all instances of $searchterm in $text with the string "^^^^^", but only if $searchterm is within an HTML tag.

For example, if $text is "<pre>Hello!</pre>", and $searchterm is "pre", I want the result to be: "<^^^^^>Hello!</^^^^^>".

Here's what I have now:
$text = preg_replace ( "/(<.*?)($searchterm)(.*?> )/", "1^^^^^3", $text );

Unfortunately, this doesn't work. For example, if $text is "<b>pre</b>" and $searchterm is "pre", the pre gets replaced. Why? Because while the "pre" may not be in an HTML tag, technically it's still between brackets - the first and last characters of $text.

Why Does This Regular Expression Fail?
Suppose I have these five lines in a file somewhere:

<p>This entry should belong to:
<select name="formInputs[id_of_neighborhood_to_which_this_belongs]">
<option value="">(No choice made)</option>
<?php getDatabaseTableValuesInOptionTags("neighborhoods", "id",
"state"); ?>
</select>

Suppose I open this file and read the contents into a string called
$string. Suppose I then give the string to this function:


function matchAllPhpFunctionsInString($subject=false) {
// 11-08-06 - this is being called in importForm

$pattern = "<";
$pattern .= "?php.*(.*); ?";
$pattern .= ">";
$pattern = "/$pattern/";
preg_match_all($pattern, $subject, $matches);
// print_r($matches);

// 09-19-06 - there is no point returning a 2 dimensional array, so we
will make
// it one dimensional.
$arrayOfPhpFunctionNames = $matches[0];
return $arrayOfPhpFunctionNames;
}

This function is suppose to find the PHP command. It works on other
pages, but not the one I've posted above. Why is that? Why would this
regular expression not find this PHP command?

Selecting With Php (regular Expression?)
first a user can pick a name ($name) and gender ($gender; value is "m" or "f"). in mysql, i'm going to have a whole blob of text. here's a short example:

[[name]] said {he|she} was going to the store today.

i can change [[name]] to $name but is there a way i can use "he" when $gender is "m' and "she" when $gender is "f"? i'm guessing with regular expression? and in the braces may contain longer things that may contain spaces, etc. like:

{hansome guy|cute girl} i'm not too good with regular expression, could someone come up with one that might work?

Finding XML With Regular Expression
I am trying to extract the XML out of a string with a regular expression and I am going crazy trying to figure it out. Here is the text String Code:

Query In Regular Expression
what does the flwg rule mean in regular expression:

([^#].*)

Regular Expression Inquiry
i've a text file which stores thousands of records.

now i find that there is reduncdant records and would like to remove them.

e.g.

Quote:

Regular Expression Inquiry
I'm processing the following sequence with length more than 100k

1 cagatgctga taaaaaagtg tgttcctcat agcatttatt taattgaaat atttcaagaa
61 cttgaatgta ctaaaaattg agacaaacag tagcaaatca taaaaaaaaa ttgaagtgaa
121 ttttacaact ggattcatgt gcctaatatt ttcattggga agtggattca tgtttaacat
181 ttccattggg <snippet>

i wrote a program

<?php
session_start();

if (isset ($_POST['seq']) )
$seq = $_POST['seq'];
else
$seq= $_GET['seq'];

$seq = preg_replace("/[s
0-9]/", "", $seq);
echo $seq;

?>

but it generates an output of fragmented sequences (i.e. partially processed
result), what is the problem?

Regular Expression : {link}
text with {link:pagehref}a link{/link}.

replace to ->

text with <a href="pagehref">a link</a>

I tried several things but nothing seems to work... e.g.

$value=preg_replace("/{link:(.+?)}(.+?){/link}/s","<a href="$1"
target="_blank">$2</a>",$value);


Copyright © 2005-08 www.BigResource.com, All rights reserved