SEF URL for categories hierarchy and articles - php

I've written small framework like code with HMVC architecture using PHP. Below are the sample SEF URLs to access.
http://domain.com/controller_name/method_name/param1/param2
http://domain.com/folder_name/controller_name/method_name/param1/param2
Above are fine and working with the fixed kind of data. But when I tried building a simple CMS, where I've category inside another category and other and so on i.e., multi-level category structure with set of articles inside, I was not able to make use of above URL. I want something like
http://domain.com/category-name/sub-category-name/sub-sub-category-name
http://domain.com/category-name/sub-category-name/article-name
Can anyone help me with snippet to achieve above.

if i did not misunderstand you want to query category by sefurl
firstly you need to convert data name to sefurl
function sefurl($s){
$s=trim($s);
$tr = array('ş','Ş','ı','I','İ','ğ','Ğ','ü','Ü','ö','Ö','Ç','ç','(',')','/',':',',');
$eng = array('s','s','i','i','i','g','g','u','u','o','o','c','c','','','-','-','');
$s = str_replace($tr,$eng,$s);
$s = preg_replace('/&.+?;/', '', $s);
$s = preg_replace('/\s+/', '-', $s);
$s = preg_replace('|-+|', '-', $s);
$s = preg_replace('/#/', '', $s);
$s = str_replace('.', '.', $s);
$s = trim($s, '-');
$s = htmlspecialchars(strip_tags(urldecode(addslashes(stripslashes(stripslashes(trim(htmlspecialchars_decode($s))))))));
return $s;
}
now you can test it
for example my categori name is "this is a test categoriğ"
sefurl($catname);
result is going tobe like "this-is-a-test-categorig"
you can use this for categori id
create a new colon in your DB table like sefurl when you set a new categoriy convert the categori name to sefurl and than set it to sefurl colon
you can query by sefurl of the categori
http://yourdomain.com/categori/this-is-a-categor-/
parse url by "/" and get "this-is-a-categor-" after that query it in your categori table.

Related

How to make a Slug from the Title

I' am creating a slug on the fly. When I review the database my slug row looks like this
laal-salaam---2002
What actually I don't want is duplicate hyphen between the words.
$crawl_slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $crawl_name);
$crawl_slug = strtolower($crawl_slug);
Thats the PHP code that handles in making the slug from the name on the fly.
The end result should be
laal-salaam-2002
Is there any other way I can achieve this issue. Thanks!
This is a Simple Function I use for years.
<?php
function to_slug($string)
{
$string = trim($string);
$string1 = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
return preg_replace("/\-+/i", "-", $string1);
}
$slug = to_slug("laal-salaam---2002");
echo $slug
?>

Remove character from php variable

I am trying to remove first 51 character of a long URL , I'm using
$sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$str2 = substr('$str',51);
Above code return blank value, it works fine if i use plan text for $str
e.g.
$str = "I am looking for a way to pull the first 100 characters from a string"
$str2 = substr('$str',10);
my url is like "https://dl.dropbox.com/u/55299544/google.html?urlc=http://example.com"
i want to get http://example.com from database to show on user page,
how can i do this?
You're making this too complicated. If you are trying to get the http://example.com from the long URL then do this.
<?php
sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$query = parse_url($str, PHP_URL_QUERY );
parse_str($query, $link);
echo $link["urlc"]; //http://example.com
?>

php dynamic meta description empty

While executing this code from a webservice, the title and keywords are shown in the meta tags as expected. The description stays empty though. How do I put dynamic array content from $array in the description? Ps eg $array[3] won't work either.
$array = $result->AAAResult->AAA->A;
$teller = count($array);
$titeltekst = "{$teller} quantity: $r";
$doc =& JFactory::getDocument();
$options = $doc->getHeadData();
$options['title'] = $titeltekst;
$options['metaTags']['standard']['keywords'] = "keywords - test";
$options['metaTags']['standard']['description'] = $array;
$doc->setHeadData($options);
You wont be able to assign an array directly to anything that expects a string, but you can use implode assuming that all the values are also strings.
$string = implode(', ', $array);
Ok I just added eg.
$string .= $v->Naam;
and;
$options['metaTags']['standard']['description'] = $string;
, if you choose to create the string on the Naam (Name) object of the Array.

Unique slugs that, if fits with slug-1, slug-2... slug-n

I am using only slug to identify a page on the website, like: example.tld/view/this-fancy-slug . This is generated from the title automatically with this function:
public static function Normalize($str)
{
$charset = "UTF-8";
$separator = "-";
$str = strtolower(htmlentities($str, ENT_COMPAT, $charset));
$str = preg_replace('/&(.)(acute|cedil|circ|lig|grave|ring|tilde|uml);/', "$1", $str);
$str = preg_replace('/([^a-z0-9]+)/', $separator, html_entity_decode($str, ENT_COMPAT, $charset));
$str = trim($str, $separator);
return $str;
}
This returns a perfect slug... but I need unique slugs. So I've to combine with mysql to check if exists a slug that fits with the created. No problem with that.
The problem is that I want to add a -1 at the final if there is ONE slug. But can be buggy if are added 3 equal slugs so... how can I manage this to go from slug, slug-1, slug-2, slug-3... slug-100, slug-n?
Thank you in advance!
Don't just check if that identical slug is present. Use a regular expression to count all the slugs that follow the pattern /(<base-regex-for-slug>)(-\d+)?/. Because you only allow alphanumerics your base regex above will be the slug itself.

How to generate seo friendly url's with php?

I want to make http://mysite.com/id255/ to http://mysite.com/gora-beach-inn/.
My php looks like:
$result = mysql_query("
SELECT id, header
FROM Article
");
while($data = mysql_fetch_assoc($result)){
mysql_query("
UPDATE Article
SET seo = '".MakeSeo($data['header'])."'
WHERE datum = '".$data['datum']."'
");
}
//Convert: "åäö" to "aao", "space" to "-", "!?" to "nothing", and all to lower case.
function MakeSeo($string)
{
???
}
Please help me with the MakeSoe function.
I use moderewrite, so I just need help to generate the url, so I can save them in my database.
To just answer your requirement ..
here you go ..
function makeSeo($text, $limit=75)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if(strlen($text) > 70) {
$text = substr($text, 0, 70);
}
if (empty($text))
{
//return 'n-a';
return time();
}
return $text;
}
You can add more filters to clean the url and may be you add some more stuff to get that url a unique.
Note: I am not saying that adding url to the database is the best way. You could achieve the same sort of functionality using other techniques, for example, mod_rewrite.
You would need to use mod_rewrite to achieve this, otherwise the other approach is the 'internal' method done by php, where you would do something similar to:
http://mydomain.com/index.php/category/dogs
the above is just a GET post, and index handles the content loading via includes / mysql etc;
Non regular expression and more flexible solution can be done via 2 arrays. Define all charcters in from and to arrays. Omitted characters will be than replaced by -
This is example of javascript function
function ToSeoFriendly(title) {
title = title.toLowerCase();
var generated = "";
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñçýčšžřľňäôabcdefghijklmnopqrstuvwxyz1234567890";
var to = "aaaaaeeeeeiiiiooooouuuuncycszrlnaoabcdefghijklmnopqrstuvwxyz1234567890-";
for (var i=0;i<title.length;i++){
generated += to.substr(from.indexOf(title.substr(i,1)),1);
}
return generated;
}

Categories