Solved

can someone tell me what is this? about regular expression

Posted on 2008-10-13
5
183 Views
Last Modified: 2012-05-05
http://fdsawfa.com/iafdqe.html?&page=286

and i want to use the preg match all or any other function
to output for me
286

soo people told me to use
preg_match_all('/(?<=[?&]page=)[^&?]*/',$yourvar,$match)){


can someone tell me exactly what eatch part is?
beacuse i want to know what i am using and not only to use it
0
Comment
Question by:mgtm3
  • 3
  • 2
5 Comments
 
LVL 8

Assisted Solution

by:nahime
nahime earned 350 total points
ID: 22706343
If you need 286 instead of use regex you can read that value in the GET array:

echo $_GET['page'];

This will output 286
0
 
LVL 8

Accepted Solution

by:
nahime earned 350 total points
ID: 22706380
This applies only if you are trying to get that value from the URL of the script otherwise you have to use that preg_match.
An example is:
<?php
$yourvar = "http://fdsawfa.com/iafdqe.html?&pippo=4&page=286";
preg_match_all('/(?<=[?&]page=)[^&?]*/',$yourvar,$match);
echo $match[0][0];
?>

Open in new window

0
 
LVL 8

Assisted Solution

by:nahime
nahime earned 350 total points
ID: 22706542
The part are:
$yourvar -> The variable containing the querystring
$match -> The variable in which the result of the regex is stored
The regular expression remove from $yourvar everything before the first ?
Remove everything between the first ? and "page="
Remove page=
Remove from the first & after "page=" until the end of string (in the example is not needed but is usefull if the string is something like http://fdsawfa.com/iafdqe.html?&pippo=4&page=286&ghgh=ghgh)

So what remain is only the number of page.
The function return 1 if matched the regex or 0 if not and in the array you have what remain of the string exploded into an array.
0
 
LVL 27

Assisted Solution

by:ddrudik
ddrudik earned 150 total points
ID: 22708420
mgtm3, here's an explanation:
----------------------------------------------------------------------
  (?<=                     look behind to see if there is:
----------------------------------------------------------------------
    [?&]                     any character of: '?', '&'
----------------------------------------------------------------------
    page=                    'page='
----------------------------------------------------------------------
  )                        end of look-behind
----------------------------------------------------------------------
  [^&?]*                   any character except: '&', '?' (0 or more
                           times (matching the most amount possible))
----------------------------------------------------------------------
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 22772246
Thanks for the question and the points.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question