Inside my webpage "http://www.my_web_page" I have something like that: img border="0" class="borderx" id="pic_15132"
I wanna know which parameter did I have to use inside preg_match_all("") to retrieve only the number.
Here is what I tried but my result is: _15132
$webpage = file_get_contents("http://www.my_web_page");
preg_match_all("#_([^>]*)[0-9]{6}#", $webpage , $matches)
foreach($matches[0] as $value)
{
echo $value . "<br>";
}
Thanks,
$webpage = file_get_contents("http://www.my_web_page");
preg_match_all("#_[^>]*([0-9]{6})#", $webpage , $matches)
foreach($matches[1] as $value)
{
echo $value . "<br>";
}
Related
I have got a PHP code to get all images in a URL. It will display the URL of the image. But there are some problems with the URL of the image obtained.
1: If the images are hosted in the root directory, it will not show the domain. For example, if the given URL is www.google.com, the URL of the image obtained is
"/logos/doodles/2014/womens-day-2014-6253511574552576.3-hp.png"
and what I need is
"www.google.com/logos/doodles/2014/womens-day-2014-6253511574552576.3-hp.png"
2: The URL obtained is always between " ". How can I remove it??
Here the PHP code
<?php
$url_image = $_GET['url'];
$homepage = file_get_contents($url_image);
preg_match_all("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", $homepage, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
echo $val[2];
echo "<br>";
}
?>
try this:
$url_image = $_GET['url'];
$homepage = file_get_contents($url_image);
preg_match_all("{<img\\s*(.*?)src=('.*?'|\".*?\"|[^\\s]+)(.*?)\\s*/?>}ims", $homepage, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
$pos = strpos($val[2],"/");
$link = substr($val[2],1,-1);
if($pos == 1)
echo "http://domain.com" . $link;
else
echo $link;
echo "<br>";
}
You can remove the "" using substr($url,1,-1);
<?
echo "Begin Function=";
echo "<br>";
$text = "2lyve: this is: 8475978474957845 948594: jfhdhfkd: just the 2lyve: beginning:";
function getTrends($text)
{
$subject = $text;
$pattern ='/(\w+:)/Ui';
preg_match_all($pattern, $subject, $matches);
foreach($matches[1] as $value)
{
print $value."<br>";
}
}
getTrends($text);
?>
The result will be:
Begin Function=
2lyve:
is:
948594:
jfhdhfkd:
2lyve:
beginning:
How do I count how many times each result is returned and rank it? Also, how to I import these results into a sql database?
PHP actually has a specific function for this purpose.
array_count_values
Your code could be changed to
<?php
echo "Begin Function=";
echo "<br>";
$text = "2lyve: this is: 8475978474957845 948594: jfhdhfkd: just the 2lyve: beginning:";
function getTrends($text)
{
$subject = $text;
$pattern ='/(\w+:)/Ui';
preg_match_all($pattern, $subject, $matches);
$findings = array_count_values($matches[1]);
foreach($findings as $value=>$occ)
{
print $value."<br>";
}
}
getTrends($text);
?>
Declare an array $map = array(); in the start of your function, and then in the place of
print $value."<br>";
put
if(isset($map[$value])) {
$map[$value]++;
} else {
$map[$value] = 1;
}
I need some help with twitter hashtag, I need to extract a certain hashtag as string variable in PHP.
Until now I have this
$hash = preg_replace ("/#(\\w+)/", "#$1", $tweet_text);
but this just transforms hashtag_string into link
Use preg_match() to identify the hash and capture it to a variable, like so:
$string = 'Tweet #hashtag';
preg_match("/#(\\w+)/", $string, $matches);
$hash = $matches[1];
var_dump( $hash); // Outputs 'hashtag'
Demo
I think this function will help you:
echo get_hashtags($string);
function get_hashtags($string, $str = 1) {
preg_match_all('/#(\w+)/',$string,$matches);
$i = 0;
if ($str) {
foreach ($matches[1] as $match) {
$count = count($matches[1]);
$keywords .= "$match";
$i++;
if ($count > $i) $keywords .= ", ";
}
} else {
foreach ($matches[1] as $match) {
$keyword[] = $match;
}
$keywords = $keyword;
}
return $keywords;
}
As i understand you are saying that
in text/pargraph/post you want to show tag with hash sign(#) like this:- #tag
and in url you want to remove # sign because the string after # is not sended to server in request so i have edited your code and try out this:-
$string="www.funnenjoy.com is best #SocialNetworking #website";
$text=preg_replace('/#(\\w+)/','<a href=/hash/$1>$0</a>',$string);
echo $text; // output will be www.funnenjoy.com is best <a href=search/SocialNetworking>#SocialNetworking</a> <a href=/search/website>#website</a>
Extract multiple hashtag to array
$body = 'My #name is #Eminem, I am rap #god, #Yoyoya check it #out';
$hashtag_set = [];
$array = explode('#', $body);
foreach ($array as $key => $row) {
$hashtag = [];
if (!empty($row)) {
$hashtag = explode(' ', $row);
$hashtag_set[] = '#' . $hashtag[0];
}
}
print_r($hashtag_set);
You can use preg_match_all() PHP function
preg_match_all('/(?<!\w)#\w+/', $description, $allMatches);
will give you only hastag array
preg_match_all('/#(\w+)/', $description, $allMatches);
will give you hastag and without hastag array
print_r($allMatches)
You can extract a value in a string with preg_match function
preg_match("/#(\w+)/", $tweet_text, $matches);
$hash = $matches[1];
preg_match will store matching results in an array. You should take a look at the doc to see how to play with it.
Here's a non Regex way to do it:
<?php
$tweet = "Foo bar #hashTag hello world";
$hashPos = strpos($tweet,'#');
$hashTag = '';
while ($tweet[$hashPos] !== ' ') {
$hashTag .= $tweet[$hashPos++];
}
echo $hashTag;
Demo
Note: This will only pickup the first hashtag in the tweet.
I want to use PHP & QueryPath to find all images in a document, then modify its src like this:
I want to change
http://test.com/test/name.jpg
to
http://example.com/xxx/name.jpg
I can find the specific class name using
$qp2 = $qp->find('body');
Now when I want to find all img on it to change the src:
foreach ($qp2->find('img') as $i) {
//here change the src
}
But when I execute
echo $qp2->html();
I see only last image. Where is the problem?
Like this?
foreach($qp2->find('img') as $key as $img) {
echo $img->html();
}
Sometimes you have to use top() or end() when you are re-using the qp object. Something like:
$qp = htmlqp($lpurl);
foreach ($qp->find('img') as $key => $img){
print_r($img->attr('src'));
$url = parse_url ($img->attr('src'));
print_r($url);
echo '<br/>';
if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
$newimg = $htmlpath . '/' . $url['path'];
$img->end()->attr('src', $newimg);
echo $img->html();
}
}
foreach ($qp->top()->find('script') as $key => $script){
print_r($script->attr('src'));
$url = parse_url ($script->attr('src'));
print_r($url);
if (!isset($url['scheme']) && !isset($url['host']) && !empty($url['path'])){
$newjs = $htmlpath . '/' . $url['path'];
echo '<br/>';
echo 'this is the modified ' . $newjs;
}
}
I want to display everything what is in a url.
So if someone use test.php?test=yes&no=why
i want to show
test = yes
no = why
but when someone use test.php?bla=blala
i want to show
bla = blala
Is this possible?
Pretty print the $_GET variable.
<?php print_r($_GET); ?>
Try this:
foreach ($_GET as $key => $value) {
echo $key . " = " . $value
}
use this php script:
foreach($_GET as $key => $value){
echo $key . " = " . $value;
}
<?php var_dump( $_GET ); ?>
will echo out everything inside $_GET
<?php var_dump( $_REQUEST ); ?>
will do everything in both a $_POST and an $_GET
You can also try
<?php
$string = "Url Variables <br />";
foreach( $_REQUEST as $key => $value ){
$string .= $key." = ".$value." <br />";
}
echo $string;
?>