how to make url rewiting works on read more link - php

I have read more link that shows part of the text. When clicked, it shows the rest of the text on next page. Here is my code:
<?php
$content = "";
$link = "linkreadmore.php";
$limit = 400;
function readMore($content,$link,$var,$id, $limit) {
$content = substr($content,0,$limit);
$content = substr($content,0,strrpos($content,' '));
$content = $content." <a href='$link?$var=$id'>More...</a>";
return $content;
?>
Now i want rewrite the URL of linkreadmore.php which lives in folder1/folder2/linkreadmore.php. Here is my rewrite rule.
RewriteRule ^/?more/([0-9]+)$ /socialM/polip/linkreadmore.php?var=$1 [L]
Then, i rewrite the read more script like this.
<?php
$content = "";
$link = "more";
$limit = 400;
function readMore($content,$link,$var,$id, $limit) {
$content = substr($content,0,$limit);
$content = substr($content,0,strrpos($content,' '));
$content = $content." <a href='/$link/=$id'>More...</a>";
return $content;
?>
It shows file not found. What am I missing here?

You have an extra = in the URL. This line should work instead:
$content = $content." <a href='/$link/$id'>More...</a>"
You‘re also missing a closing } for your function, but if the code works, that’s probably just a typo in the question.

Related

PHP regex to exactly obtain a string I want

I have a code for embedding a link for iframe.
$post_contetn = explode('htt',$content);
$content_with_link = $post_contetn[0];
$link = 'htt'.$post_contetn[1];
But the problem is that, if I write
http://www.espn.com was great
then it links "was great" is part of the $link.
How can I change (perhaps use regex) to only include the actual url?
======
If I incorporate siam's answer, should it be
$regex = '/https?:\/\/.*?(?=\s)/';
$post_contetn = preg_match($regex, $content, $linkarray);
$content_with_link = $post_contetn[0];
$link = $linkarray[0]
echo $content_with_link;
I then edited to
preg_match($regex, $content, $post_contetn);
$content_with_link = $post_contetn[0];
$link = $post_contetn[0]
echo $content_with_link;
But the error still occurs at echo line.
Try using the following regex :
(?:https?:\/\/\S+)?\S+\.\S+\.?\S+
see demo / explanation
PHP
<?php
$content = 'http://www.espn.com was great';
$regex = '/(?:https?:\/\/\S+)?\S+\.\S+\.?\S+/';
preg_match($regex, $content, $post_contetn);
$link = $post_contetn[0];
echo $link;
?>

Can we Use Replace By str_replace in a code fetched from remote url

i got Source Code From Remote Url Like This
$f = file_get_contents("http://www.example.com/abc/");
$str=htmlspecialchars( $f );
echo $str;
in that code i want to replace/extract any url which is like
href="/m/offers/"
i want to replace that code/link as
href="www.example.com/m/offers/"
for that i used
$newstr=str_replace('href="/m/offers/"','href="www/exmple.com/m/offers/',$str);
echo $newstr;
but this is not replacing anything now i want to know 1st ) can i replace by str_replace ,in the code which is fetched from remote url and if 'yes' how ...? if 'no' any other solution ?
There will not be any " in your $str because htmlspecialchars() would have converted them all to be " before it got to your str_replace.
I start assuming all href attributes belong to tags.
Since we know if all tags are written in the same way. instead of opting for regular expressions, I will use an interpreter to facilitate the extraction process
<?php
use Symfony\Component\DomCrawler\Crawler;
$base = "http://www.example.com"
$url = $base . "/abc/";
$html = file_get_contents($url);
$crawler = new Crawler($html);
$links = array();
$raw_links = array();
$offers = array();
foreach($crawler->filter('a') as $atag) {
$raw_links[] = $raw_link = $atag->attr('href');
$links[] = $link = str_replce($base, '', $raw_link);
if (strpos($link, 'm/offers') !== false) {
$offers[] = $link;
}
}
now you have all the raw links, relative links and offerslinks
I use the DomCrawler component

Replace the page from a url using php

I have this type of urls stored in a php variable:
$url1 = 'https://localhost/mywebsite/help&action=something';
$url2 = 'https://localhost/mywebsite/jobs&action=one#profil';
$url3 = 'https://localhost/mywebsite/info&action=two&action2=something2';
$url4 = 'https://localhost/mywebsite/contact&action=one&action2=two#profil';
I want to replace the page help, jobs, info, contact with home in a very simple way, something like this:
echo replaceUrl($url1);
https://localhost/mywebsite/home&action=something
echo replaceUrl($url2);
https://localhost/mywebsite/home&action=one#profil
echo replaceUrl($url3);
https://localhost/mywebsite/home&action=two&action2=something2
echo replaceUrl($url4);
https://localhost/mywebsite/home&action=one&action2=two#profil
So here is the solution i found:
function replaceUrl($page){
$pieces = explode("/", $page);
$base = '';
for ($i=0; $i<count($pieces)-1; $i++) $base .= $pieces[$i].'/';
$hash = strpbrk($pieces[count($pieces)-1], '&#');
return $base.'home'.$hash;
}
You'll want to add something like
RedirectMatch 301 help(.*) home$1
to your .htaccess file. I'm not sure PHP is the correct tool for the job.
If you actually want to modify a string with the value of that (which is what your tags and.. comments indicate), you'll want to do:
$url = "https://localhost/mywebsite/help&action=something#profil"
$url = str_replace("help", "home", $url);
echo $url; // https://localhost/mywebsite/home&action=something#profil

str_replace PHP script can't handle foreign characters such as umlauts robustly

The following script does not always correctly catch and convert foreign characters. Could someone show me what I'm missing to get it to be more robust?
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",$content),"</div>";
include("../index_footer.inc.php");
?>
New information: Pekka, you gave me the idea to check how the page emits without str_replace():
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "XXXXXX.html?no_body=1";
$content = file_get_contents($url,'r');
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",$content,"</div>";
It seems the problem lies elsewhere because I get the same mangling even without using str_replace()! If you can help me get this sorted out, I would sure appreciate it. I have seen your wish list. ;)
Did you include the charset in php?
try this:
header('Content-Type: text/html; charset=utf-8');
If not working check if your file is already saved in utf8 before str replace:
utf8_encode ( string $data );
In the opposite case use:
utf8_decode( string $data );
Hope it helps!
Thank you SBO - It sure did help! I simply changed the code to:
<?php
include("../index_head.inc.php");
$content = implode("",(#file("current.txt")));
$url = "http://XXXXXX.html?no_body=1";
$content = file_get_contents(utf8_encode($url),'r');
if (isset($_GET['showcurrent']) && $_GET['showcurrent'] == '')
{
$content = substr($content,1,strpos($content,"<hr ")-1);
}
else
{
$content = str_replace("<br style=\"clear:both\" />\n</p>", "</p>",$content);
$content = str_replace("ck1\"><img", "ck1\" target=_blank><img",$content);
};
$content = str_replace("<h3>current</h3>", "",$content);
echo "<div id=\"service\" style=\"width: 660px;padding-left:5px\">",str_replace("current.html","current.html",utf8_decode($content)),"</div>";
include("../index_footer.inc.php");
?>
and everything is working fine.
Thank you very much for your help.

how to print url from html code in php when url contain spaces

See i have an url in a html code
play
Now i want to print this url as it is written in a php page
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can see that between the url 05 Dabangg Reloaded their is space. I made this program to print url from this html code..
$str = "play";
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
if (preg_match_all($pattern,$str,$matches))
foreach($matches[1] as $data)
{
$str=$data;
echo $str;
}
Then i am getting this
http://b48.ve.vc/b/data/48/3746/05
please do not mention on foreach($matches[1] as $data) line bcoz i am using it with so many urls.. I just want to know how to print the whole url in this format.
http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
Spaces are become a huge matter.. Do not know how to fix it..
What i need to add inside
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
For making it completely workable.
Please suggest me any idea.
$str = 'play';
$arr = explode("\"", $str);
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;=?#.-]+)[^\w#$&+,\/:;=?#.-]*?`i';
$url = preg_grep($pattern,$arr);
$url = implode('',$url);
Output: $url = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3'
Update: 2nd Solution [Reference-DOMElement].
$str = 'play';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
$search_item = $DOM->getElementsByTagName('a');
foreach($search_item as $search_item) {
$url = $search_item->getAttribute('href');
}
echo $url; //Output: http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
You can str_replace each one -space- with %20 for encoding your URL
<?php
$url_org = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3';
$url_edited = str_replace(" ", '%20', $url_org);
?>
HERE
This will work.

Categories