PHP extract String after pattern - php

I want to cut a html link with php.
The html link is everytime the same pattern
domain.com/forum/members/84564-name.html
I want to get the 84564 from the name.
the /forum/members/ is everytime the same.
and the "-" after the user-id is also everstime the same.
Can you help me to extract the user id?

I'm going to assume the user id may not always be 5 digits.
$domainSplit = explode("/", $theDomain); // split into parts
$theIdSplit=explode("-", $domainSplit[3]); // split 84564-name.html
$id=$theIdSplit[0];

Related

How do I find the second, or third instance of a character in a string in PHP

I am trying to parse out the middle of the referrer information just to check from where the form submission is coming from.
Consider that the referrer is "https://www.somewebsite.com/someform.php
And what I want to extract is the part between those first two "//" and the next one "/".
I can get the string position, but how can I do a quick piece of code that will just parse out the part I need without several steps?
$ref=strpos($_SERVER['HTTP_REFERER'],'://');
gives me 5 (as the position where the "://" is found.
I want to use something like substr(string,start,length) the part out that is needed to get the pure referrer
the part that says "www.somewebsite.com"
without all the extra lines of code
used it
echo $_SERVER['HTTP_HOST'];

PHP get 5 additional characters after a specified string of a particular page and list them

Note: I have edited the post on 2017/08/20
I'm trying to obtain a list of product page's URL that goes "www.example.com/product/11111/".
There are over 200 different products available and each of them has its own product page, I want to print out each product in a PDF file.
On "www.example.com/productlist/", there are URLs that lead to each product's page.
So, what I'm trying to do is
Obtain URLs that I need from "www.example.com/productlist/"
Generate PDF files of URLs that I have obtained
Insufficient Information: You did not provide me with much information about the code you already have and how the website will get the 200 URLs, so I can't write the whole code because it depends on the way your website will get the links from.
If you explain more about how the website is supposed to get the links, I will help you put them into an array and save them into a file and implement the rest of the code!
1-) Thing I understood is getting the last 5 characters
As you just want the last 5 characters, not the whole last part, you can do something like this.
$string = "http://example.com/folder/example/1234567"; //your link
$characters = strlen($string); //gets the characters count
$letters = 5; //edit 5 to show more or less manually
$code = substr($string, $characters - $letters, $characters);
echo $code; //will show the last 5 characters
I am always here to help. Good luck!
Start with parse_url().
$parts=parse_url("http://example.com/folder/example/12345");
That will give you an array with a handful of keys. The one you are looking for is path.
Split the path on / and take the last one.
$path_parts=explode('/', $parts['path']);
Your random numbers can now be stored like:
$number = $path_parts[count($path_parts)-1].

Extract domain name from affiliate URL using PHP

Here is the format of affiliate URL I have http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails
If you see it has 2 URLs:
first URL: is for vcommission.com and
Second URL: netmeds.com
I have CSV file with lot of rows. Each rows may have different second URL. I wanted to get second URL for each rows. First URL is also not static as for different CSV, this would also different.
How can I get second URL?
Some basic string parsing like this should give you an idea.
$url='http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails';
list($u,$q)=explode('url=',urldecode($url));
$o=(object)parse_url($q);
echo $o->host;
A good way to find the domain for a URL is with parse_url
Unfortunately due to the way your data is stored this is not really an option however you may be able to use some sort of regex to find contained web addresses in the query string
<?php
$url = "http://tracking.vcommission.com/aff_c?offer_id=2119&&url=http%3A%2F%2Fwww.netmeds.com%2F%3Fsource_attribution%3DVC-CPS-Emails%26utm_source%3DVC-CPS-Emails%26utm_medium%3DCPS-Emails%26utm_campaign%3DEmails";
$p = parse_url($url);
$pattern = "/www[^%]*/";
preg_match($pattern, $p['query'], $result);
var_dump($result);
You may need to adjust the regex pattern based on how the other data presents itself.

how to get the spesific word from different string in php

for getting spesific word that i will use it for search term.
if i have a random word like :
$str="297819 - 123C, WHITLEY ROAD";
that $str is got from adrress which is got from autocomplete jQuery, every address is defined into 2 part. first 297819 are "code", and 123C, WHITLEY ROAD is the "address".
i know hot to get the "code" with this following code :
$b = substr($str,0,strpos($str,"-")-1)
it will be return the "code" only.
guys can you help me how to get the address?
p.s every code and address will be disperated by -
Code and address separate by - so you can split it Using explode() as
$str="297819 - 123C, WHITLEY ROAD";
$data=explode("-",trim($str));// split string with -
echo $code=trim($data[0]);// get code
echo $address=trim($data[1]);// get address
And use trim() to remove unnecessary space

PHP (Change the pageid to the page name.)

I am making the dynamic website in PHP. While code is running good using pageid.
The url is now like www.google.com/pageid=1
Now, i want to change the url for 1 level pages as www.google.com/page1
for level 2 as www.google.com/page1/page2
for level 3 as www.google.com/page1/page2/page3
While unique address is stored in the my table as page1,page2,page3.
How can it be possible to change url at run time. Please give the examples and comments. So that it may helpful to understand.
Also i want to know if it is possible using the .htaccess file. If possible How .htaccess %{REQUESTED_FILE}% will reach out for my unique url's that are stored in the database.
$URL= "www.google.com/page1"
1) split the string with '/'
$data = explode("/",$URL);
$data[1] will have page1 value
2) replace the string page with ""
$pageId = str_replace("page","", $data[1]);
3) whatever is left is the page id
pageId has the value now.
Use that to query the database.
Another way is to use regex to extract the number from page1 and use it where-ever you want to use it.

Categories