Fetching specific URL with preg_match - php

This is my code :
$patt = "#href=\"(.*?)\"#";
preg_match($patt,$data,$match);
echo $match[1];`
i.e. theres a URL in the HTML code of the page $data
<a href="http://aba.ai/iEU9x">
I want to grab this link above. Thanks

Related

Get Text From URL in PHP

How can i call in php words from url?
Example 1
http://myurl.com/keyword-one
I want to display "Keyword One" as page title in php
Example 2
http://myurl.com/keyword/keyword-two
I want to display "Keyword Two" as page title in php
Thank you! :-)
the simplest solution in those cases would be using (untested code, but should work):
<?php
//get path
$urlPath = $_SERVER["REQUEST_URI"];
//get last element
$end = array_slice(explode('/', rtrim($urlPath, '/')), -1)[0];
//replace dashes with spaces and display
echo ucwords(str_replace('-',' ',$end));

How to change every link href value to different url

I'm trying to change every url in a paragraph to encrypted url, so i can track all clicks on outgoing links for e.g
$paragraph = "<p>This is an example <a href='example.com'>My Website</a></p>
<h2>another outgoing link</h2>";
I want to get every above url and encrypt them and replace them.
Suppose i got example.com change it into mywebsite.com/track/<?=encrypt("example.com")?>. and finally paragraph should look like this
$paragraph = "<p>This is an example <a href='mywebsite.com/track/29abbbbc-48f1-4207-827e-229c587be7dc'>My Website</a></p>
<h2></h2>";
Here is What I've tried
$message = preg_replace("/<a([^>]+)href=\"http\:\/\/([a-z\d\-]+\.[a-z\d]+\.[a-z]{2,5}(\/[^\"]*)?)/i", "<a$1href=\"encrypted url", $message);
Using preg_match or preg_replace for HTML string is a very bad idea you should use,DOMDocument
Try this code snippet here
<?php
ini_set('display_errors', 1);
$paragraph = "<p>This is an example <a href='example.com'>My Website</a></p>
<h2></h2>";
$domDocument= new DOMDocument();
$domDocument->loadHTML($paragraph,LIBXML_HTML_NOIMPLIED|LIBXML_HTML_NODEFDTD);
//getting all nodes with tagname a
$results=$domDocument->getElementsByTagName("a");
foreach($results as $resultantNode)
{
$href=$resultantNode->getAttribute("href");//getting href attribute
$resultantNode->setAttribute("href","mywebsite.com/track/"."yourEncoded:$href");//replacing with the value you want.
}
echo $domDocument->saveHTML();

How to fetching data with php from a website and put its data to variable?

I am going to read a website data and put its data in a variable.
$site=file_get_contents($url);
I am going to work with $site variable and read data from $url:
for example I am going to fetch data from this url
http://en.wikipedia.org/wiki/List_of_states_and_territories_of_the_United_States
How Should I do it?
I used explode function but I want to get center of a div for example when there is a div I need to get what is in div
<div id="div1">
...I need to get here between open and close tag of div
</div>
Using regular expression:
<?php
$url = "http://somesite.com/";
$begining_match = '<body>';
$end_match = '</body>';
$file_contents = file_get_contents($url);
$extracted_contens = preg_replace("/\$begining_match([a-z0-9\.])\$end_match/i", "\\1", $file_contents);
echo $extracted_contents;
?>
Taken from: http://www.webdeveloper.com/forum/showthread.php?37464-possible-to-have-file_get_contents-only-grab-between-two-things

Multiple GETs in URL at the same time?

I'm trying to GET variables out of an URL, but everytime the URL already has a query, it gets overwritten by the next query. Example:
I have a link;
Page 34
When you click on that link this link becomes visible;
Item 45
But when I click that link, the other one get overwritten so the URL looks like;
www.domainname.ext/?item45
But I want it to look like;
www.domainname.ext/?page=34&item45
Any way to do that?
Thanks in advance!
From within a given "page", you need to store the page ID and use that stored value when you create "item" links.
<?php
$pageID = $_GET['page'];
// ....
?>
Item 45
You can also youse http_build_query(); to append more params to your URL
$params = $_GET;
$params["item"] = 45;
$new_query_string = http_build_query($params);
PHP http_build_query
for instance:
$data = array('page'=> 34,
'item' => 45);
echo http_build_query($data); //page=34&item=45
or include amp
echo http_build_query($data, '', '&'); //page=34&&item=45
<a href="?page={$page->id}&item={$item->id}">
Page {$page->id}, Item {$item->id}
</a>
When outputting the links, you'll have to include all of the relevant query string parameters; there's no automatic "merge".
If you could change your server-side stuff to using RESTful URLs, you could get this sort of behavior. E.g., starting with
http://www.domainname.ext
this link
<a href='page34'>Page 34</a>
would take you to
http://www.domainname.ext/page34
after which this link
<a href='item43'>Item 43</a>
would take you to
http://www.domainname.ext/page34/item43
...but that requires quite a big change in your server-side stuff.

How to know whether i have clicked the particular link in the next page

I have created certain links using a for loop in PHP.
foreach($storage as $val)
{
if($val!="")
{
echo "$val";
echo "Link";
}
}
Page will look like
content1 Link to b.html
content2 Link to b.html
content3 Link to b.html
content4 Link to b.html
...
...
In the next page I want to retrieve the correct content based on the link clicked.
Example: If I click the second link the content to be retrieved is "content2".
How can I accomplish this?
Create your links like that:
echo 'Link'
Then, on the other page, use this code:
$content = isset($_GET['content']) ? $_GET['content'] : '';
Then $content contains whatever was passed in the URL - or an empty string if the param was missing.
You could set Get parameters so that your links are to b.html?var=content2
Then you can just read the Get Parameters
echo $val ."Link";
and you can then view this value with
echo $_GET['var'];

Categories