News Article's title converted to a slug URL

Hello,

I am new here and to PHP coding. I just want to say thank you in advance to anyone who is nice enough to take the time to help me out.

I recently created a news article program that lets the user create a new article for a website and then it creates a webpage from a template.

It takes the actual date and adds it to the filename for the url of the article, and it works great so far. But Google prefers that news articles actually have the title/subject of article in the url. I can make the subject go into the file name but because of the spaces and maybe other non url friendly characters, it gives me an error.

I can find all over the net examples of string replace and understand how it’s done. What I am not able to do is take what I programmed already and convert the user inputted title into the URL string.

Below is a piece of the code and how it works now. What I need help with is to take the posted string subject and create a new variable that is a slug of the title/subject and use that instead of the article ID which is the date. If I can just take $subject = $_POST[subject]; and create a new variable called $slug_url which is the subject but with a slug URL friendly string, then I can place it in the filename creation section…
Again thanks in advance and I appreciate it very much!!!

[php] <?
include (“template.inc”);
include (“config.php”);

$subject = $_POST[subject];
$summary = $_POST[summary];
$passwd = $_POST[passwd];
$date = $_POST[date];
$body = $_POST[body];
$article_id = $_POST[article_id];

#foreach($GLOBALS as $a => $b){	print "<li>$a => $b";}

$summary_template = "t_summary.html";
$article_template = "t_article.html";
$max_summary = 5;

function summary_page ($subject, $date, $summary, $article_id)
{
	global $summary_template;
    	$t = new Template();
    	$t->set_file("SummaryPage", $summary_template);
	$article_url = "http://www.mydomain.com/articles/NFL/article_NFL_".$article_id.".html";
	$date = nl2br($date);
	$summary =  nl2br($summary);	 
	$t->set_var( array(
			"subject" => $subject,
			"date"    => $date,
			"summary" => $summary,
			"article_url" => $article_url
			));
	$t->parse("Summary", "SummaryPage");
	return $t->get_var("Summary");
}

function main_page ($subject, $date, $summary, $article_id, $body)[/php]

I usually do something like this:

[php]$slug_url = str_replace(" “,”-",$subject);
$slug_url = preg_replace("~[^a-zA-Z0-9-_]~","",$slug_url);[/php]

First command replaces any space to dash, and second command removes any special characters - leaving only letters, numbers and also dashes and underscores. If you want to allow other characters, you can add them to this regular expression (some may require escaping with backslash).

Thank you… I will try it today and post how it went…

Again, thank you for your time in helping me…

BBS

Johnny

Hey phphelp!!!

Thanks a bunch!!! So far it works great with one exception… Now another thing I picked up with some help :wink:

The issue I have now is that it creates the file fine and adds the new slug URL, but when it creates the file it now leaves out the article ID in the file name. The summary page shows the link including the article ID and slug url properly, but I must be doing something wrong… let me know if you can help me out… again… Maybe it is something you can point out ina quick second… :wink:

[php] <?
include (“template.inc”);
include (“config.php”);

$subject = $_POST[subject];
$summary = $_POST[summary];
$passwd = $_POST[passwd];
$date = $_POST[date];
$body = $_POST[body];
$article_id = $_POST[article_id];

#foreach($GLOBALS as $a => $b){	print "<li>$a => $b";}

$summary_template = "t_summary.html";
$article_template = "t_article.html";
$max_summary = 5;

/* 	Takes the $subject and converts it to a Slug URL string - Info provided by www.phphelp.com */
$slug_url = str_replace(" ","-",$subject);
$slug_url = preg_replace("~[^a-zA-Z0-9\-\_]~","",$slug_url);

function summary_page ($subject, $date, $summary, $article_id, $slug_url)
{
	global $summary_template;
    	$t = new Template();
    	$t->set_file("SummaryPage", $summary_template);
	$article_url = "http://www.mydomaine.com/articles/NHL/article_".$article_id."_NHL_".$slug_url.".html";
	$date = nl2br($date);
	$summary =  nl2br($summary);	 
	$t->set_var( array(
			"subject" => $subject,
			"date"    => $date,
			"summary" => $summary,
			"article_url" => $article_url
			));
	$t->parse("Summary", "SummaryPage");
	return $t->get_var("Summary");
}

function main_page ($subject, $date, $summary, $article_id, $slug_url, $body)
{
	global $article_template;

            $t = new Template();
            $t->set_file("ArticlePage", $article_template);
            $article_url = "article_".$article_id."_NHL_".$slug_url.".html";
            $date = nl2br($date);
            $summary =  nl2br($summary);
            $body =  nl2br($body);
            $t->set_var( array(
                            "subject" => $subject,
                            "date"    => $date,
                            "summary" => $summary,
                            "body" => $body,
                            "article_url" => $article_url
                            ));
            $t->parse("Article", "ArticlePage");
            return $t->get_var("Article"); 
}

function add_article($filename, $news)
{

	if(file_exists($filename)){
		$fh = fopen($filename, "r");
		$old_news = fread($fh, filesize($filename));
		fclose($fh); 
	}

	/* TODO: Multipage articles
		preg_match_all("<!--ARTICLE PAGE=(\d*)-->", $old_news, $matches;
	
		if( count($matches[0]) >= $max_summary){
			$oldfilename = $filename.($matches[0][0]+1);
		} 
	*/

	$fh = fopen($filename, "w");
	$news = stripslashes($news);
	fwrite($fh, "\n<!--ARTICLE-->\n$news $old_news");
	fclose($fh);
}

if(strcmp($subject, "")){	
	if(!(strcmp($passwd, $password))){	
		add_article("article_summary.html", summary_page($subject, $date, $summary, $article_id, $slug_url));
		add_article("article_$article_id_NHL_$slug_url.html", main_page($subject, $date, $summary, $article_id, $slug_url, $body));
		echo "<p> <a href=article_$article_id_NHL_$slug_url.html>Article</a> has been added! <p>";
	}else{
		echo "<p><b> Password is wrong! </b>";
	}
}

?>[/php]

One other thing I wanted to ask you… What would be the proper syntax if I wanted to add a " ’ " to the character replace string ($slug_url = preg_replace("~[^a-zA-Z0-9-_]~","",$slug_url):wink:

This is in case of a conjunction like “What’s, Don’t” and so on… it would replace the character " ’ " with either a hyphen “-” or just delete the space altogether… Hyphen would be easier I imagine… Also would that need a backslash in order to escape?

Thanks for the info… I like how this site not only helps you but also explains it for learning and educating purposes… I am self taught in PHP and help like that is ‘Golden’ to me… :slight_smile:

I found out how to do this… I found it on php.net… In case you have other members reading this thread I pasted the link below… It is a character that needs a blackslash for escaping… I new that too but think I had a blonde moment… lol Hey! I goto school fulltime and work fulltime…Give me a break… :stuck_out_tongue: kidding!

preg_replace function:
http://php.net/manual/en/function.preg-replace.php

Syntax for PHP:
http://www.php.net/manual/en/language.types.string.php

Do you mean what when it create file, the Article ID is not included in this file name?

Yes, I apologize… I knew I was confusing there…
The actual file name is not being rendered with the aritcleID, but the slugURL is being included…
I am wondering what I am doing wrong besides just getting assistance on how to fix this…

Thanks

Yes that is the issue… I just double checked and the file name is not including the article_ID now…

The file name it outputs is something like this:
“article_Fehr-close-to-becoming-head-of-NHLPA-.html”

It should be like this:
“article_2009_03_29_4454_NHL_Fehr-close-to-becoming-head-of-NHLPA-.html”

Can it be becuase of the numbers in the preg statmepreg_replace("~[^a-zA-Z0-9-_]~","

thanks again…

I am not sure why the id is not inserted in your filenames. You can try to change your code to this:

[php]add_article(“article_”.$article_id.“NHL”.$slug_url.".html", main_page($subject, $date, $summary, $article_id, $slug_url, $body));[/php]

That was it!!! Works Great!!! 8) :stuck_out_tongue:

It was a syntax error on my part and once I made the changes you suggested it all made sense…

THANK YOU!!! I will credit this site on the php page… Take care…

Sponsor our Newsletter | Privacy Policy | Terms of Service