how to avoid change in url address in rtl languages - php

hey guys im working on a task to make my story's links like this
http://localhost/mycms/article/test/
i used :
$mtitle = str_replace("\"", "'", $title);
$slug_title = mysql_real_escape_string($mtitle);
and a href link to show story's title
in other php file i used two arrays as a moderator for google tab
$urlin = array(
"'(?<!/)modules.php\?name=News&file=article&title=([a-zA-Z0-9_-]*)'",
"'(?<!/)modules.php\?name=News&file=tags&tag=([a-zA-Z0-9_-]*)'"
);
$urlout = array(
"article/\\1/",
"article/tags/"
);
and it automatically change urls
but when it goes to RTL languages such as arabic , it failed
e.g. :
http://localhost/CMS/article//صفحات
while it should be like this :
http://localhost/CMS/article/صفحات/
i tried different ways to correct this but none of them worked

Arabic is right to left and English is left to right, could this be any part of the problem?
Just guessing.
If you want to break apart the elements use
$urlparts=explode("/",$url);
$urlparts is now an array that has each element.
$urlparts[0]="http:";
$urlparts[1]=""; // (because of //)
$urlparts[2]="localhost";
$urlparts[3]="CMS";
$urlparts[4]="article";
$urlparts[5]=""; // (because of //)
$urlparts[6]="صفحات"; //(persian for pages)
You can put it back together I presume?

I suggest splitting the URL, and then replace each part, then re assemble the string as one process.

I had the same problem as I was using the url below and wanted the _3904 at the end of the url before the slash.
http://9th-class.result.pk/punjab/urdu/short-questions/chapter-4_شاعروں-کے-لطیفے_3904/
But it was not working for me so I have changed the url to
http://9th-class.result.pk/punjab/urdu/short-questions/chapter-4_شاعروں-کے-لطیفے_c3904/
and it worked perfectly.

Related

How can I use special character (?) in URL

I have developed a website using Raw PHP. I have used special character (? and ~) in URL. Example: http://webdeveloperszone.com/?~=portfolio .
Now, I want to rebuild this website using wordpress or codeigniter. I want to keep all the existing link live, because google has cached those links. That means, I have to create links which supports special character (? and ~).
Can any one tell me how can I do this?
PHP
string urlencode ( string $str );
string urldecode ( string $str );
JavaScript
encodeURI(uri);
decodeURI(encodeURI(uri));
For codeigniter
Have a look on
https://ellislab.com/codeigniter/user-guide/general/urls.html
Enable query string
And change
$config['controller_trigger'] = 'c';
To
$config['controller_trigger'] = '~';
In wordpress just you need to create page.
and then change post name from database as you like.
Create page named portfolio.
save and publish it.
then go to phpmyadmin.
open wp_posts.
get the record for perticular page.
and change post_name to your slug ?~=portfolio.
That's it.

Preg_Replace Change URL

I am trying to grab content from another one of my site which is working fine, apart from all the links are incorrect.
include_once('../simple_html_dom.php');
$page = file_get_html('http://www.website.com');
$ret = $page->find('div[id=header]');
echo $ret[0];
Is there anyway instead of all links showing link to have the full link? using preg replace.
$ret[0] = preg_replace('#(http://([\w-.]+)+(:\d+)?(/([\w/_.]*(\?\S+)?)?)?)#',
'http://fullwebsitellink.com$1', $ret[0]);
I guess it would be something like above but I dont understand?
Thanks
Your question doesn't really explain what is "incorrect" about the links, but I'm guessing you have something like this:
<div id="header">Home | Sitemap</div>
and you want to embed it in another site, where those links need to be fully-qualified with a domain name, like this:
<div id="header">Home | Sitemap</div>
Assuming this is the case, the replacement you want is so simple you don't even need a regex: find all href attributes beginning "/", and add the domain part (I'll use "http://example.com") to their beginning to make them absolute:
$scraped_html = str_replace('href="/', 'href="http://example.com/', $scraped_html);

PHP , htaccess: apply page title in URL

I want to apply the page HTML title in the URL
for example in here (stackoverflow) the url is something like that:
http://stackoverflow.com/questions/10000000/get-the-title-of-a-page-url
you can see the "get-the-title-of-a-page-url" part which is the page title
what i mean is when the user go to spowpost.php?post=1
the actual url that shows up when the pages load will be
spowpost.php?post=1$title=..the_title..
how can i do that?
EDIT: i was thinking about htaccess , but i don't know this very well so tutorial would help for this CERTAIN case..
You can use .htaccess for this. For example:
RewriteEngine On
RewriteRule ^questions/(\d+)/([a-z-]+) index.php?id=$1&title=$2 [L]
Your PHP page (index.php) receives the id and title as parameters in $_GET[]:
echo $_GET['title'];
// get-the-title-of-a-page-url
You can then use that (or the id, which is easier) to retrieve the correct item from your data source:
// Assuming you didn't store the - in the database, replace them with spaces
$real_title = str_replace("-", " ", $_GET['title']);
$real_title = mysql_real_escape_string($real_title);
// Query it with something like
SELECT * FROM tbl WHERE LOWER(title) = '$real_title';
Assuming you do have an id parameter in the URL of some sort, it's easier to query based on that value. The title portion can be used really only to make a readable URL, without needing to act on it in PHP.
In reverse, to convert the title to the format-like-this-to-use-in-urls, do:
$url_title = strtolower(str_replace(' ', '-', $original_title));
The above assumes your titles don't include any characters that are illegal in a URL...
$article_link = "http://example.com/spowpost.php?post=$postid&$title=$url_title";
Or to feed to .htaccess:
$article_link = "http://example.com/spowpost$postid/$url_title";
From what I understand, your title will be passed to the page as part of the URL. To show it in the title bar, put this in the section:
<?php $title=urldecode($_GET["title"]); echo "<title>$title</title>"; ?>
You might need to change parts of this, for instance dashes to spaces or something. If that is the case, use PHP's str_replace function: http://php.net/str_replace
Not sure about what's the problem you are facing but just according to what you say in your post the anwser would be:
1.You take the ID from the URL.
2.You search in your database for the original title
3. And then display it in the tag in the of your HTML.
Clarify if you have problem with any of the previous points.

Extracting site title from URL

I'm trying to find a away to extract a site title from a URL entered into a field in PHP. For example, if the user were to enter the URL http://www.nytimes.com/2009/11/05/sports/baseball/05series.html, I would want "New York Times" or "NY Times" or something along those lines.
I know it's fairly easy to extract the title of the WINDOW... for example, the URL I linked would have the title "Yankees 7, Phillies 3 - Back on Top....", but this is exactly what I don't want.
For clarification, this is for adding sources to a quote. I want to be able to add a source to quotes without a huge page URL and not just a link that says "Source".
Can anyone help me with this? Thanks in advance.
$source = parse_url('http://www.nytimes.com/....', PHP_URL_HOST); // www.nytimes.com
There is no such thing as a "site title" , you can get
the domain name (and then the owner name)
the page's title
I see you have the meta tag "cre" with the value "The New York Times" but you won't find it everywhere
You can do one thing : extract the domain name from the URL, and then get the first page's title
"http://www.nytimes.com/" will give you "The New York Times - Breaking News, World News & Multimedia"
Build a list of URL prefixes to site names, and check for each prefix in turn from longest to shortest.
You'd surely need a lookup table mapping domains (nytimes.com) to your titles "NY Times" in which case it would be easy to do.
If you want to have a method that will work on any link from any domain, then it is a bit harder as PHP in itself is not going to be able to work out what is a uniform title as it will vary from site to site.
You can explode the URL easily enough, but how then would you be able to dissect nytimes into "NY" and "TIMES".
You may be able to find a web service that allows you to feed in a domain and get back a site title, but I do not know of one.
You are best off simply quoting the domain, trimmed like "NYTIMES.COM" as the source, or "NYTIMES".
You would want to use file_get_contents() then run a match to check the text between any <title></title> tags - that then would be your title that you display.
Using parse_url wouldn't return the actual page title.
Something like:
<?php
$x = file_get_contents("http://google.com");
preg_match("/<title>(.+?)<\/title>/", $x, $match);
echo $match[1];
?>
Use the Simple HTML DOM Parser. Here is an example:
require "simple_html_dom.php";
$url = "http://www.google.com";
$html = file_get_html( $url );
list( $title ) = $html->find( 'title' );
echo strip_tags( $title ); // Output: "Google"

Text Display in PHP

if i stored data in DB which contains urls (for example : Go thorugh this link http://www.google.com).
when i display that data in browser, i want to display that data like " Go through this link http://www.google.com ". but that url which looks like anchor link...
if you didn't get this..open google chat...send some msg to anyone like http://google.com..if u send plain text like http://google.com,but it shows with hyper link..to that url..
i want this functionality in PHP technology...how can we implement this
thanks in advance...
So, you want to convert the urls to links in php? See the first result, or answers to same question in stackoverflow.
If I understood this correctly you want to transform URLs in a text to links automatically, without going further into details a crude (very crude) regexp should do it for now:
$textWithLinks = preg_replace('#(http|ftp)s?://[^\s]+#i', '$0', $textWithUrls);
function add_href ($text) {
return preg_replace('/((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/', '$0', $text);
}
Expression taken from http://rickyrosario.com/blog/converting-a-url-into-a-link-in-csharp-using-regular-expressions/

Categories