Forum Moderators: coopster

Message Too Old, No Replies

generate tag or title from folder name

generator, php, folder

         

maxweels

1:35 am on Jul 11, 2008 (gmt 0)

10+ Year Member



Hello everybody first of all i have to tell you that's my english
is not very good im french and newbies on php

so this is my problem :

I have file called config.php :

<?php

$site_url="http://mysite.com/folder-name/";

$tag_by_default="keyword";

$site_title="keyword";

$tag_list="keyword1,keyword2,keyword3";

?>

i want to generate site_title or tag_by_default from "folder name" then i can save time
i have many many pages to made

i have try something like this $tag_by_default="{page_title}";
but no result

if somebody can help me it would be great

Thank you carlos.

g1smd

2:18 am on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It is possible.

You'll need to make sure that the folder name does not have any spaces in it. Avoid underscores too, if you can.

You'll need to read that name, and strip out the word separators, changing them to spaces. You'll also need to Upper Case at least the first word.

The data will go to become the anchor text.

maxweels

5:36 am on Jul 11, 2008 (gmt 0)

10+ Year Member



Hello thanks for your fast reply
i can give you example folder name :

http://example.com/avril-lavigne-girlfriend/

i think i understand what you say but i don't know how to do it if you can give me example then i can make a batch file or something like that
thank you .

[edited by: dreamcatcher at 6:37 am (utc) on July 11, 2008]
[edit reason] use example.com. Thanks. [/edit]

g1smd

10:16 am on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



There are quite a few ready-made example code snippets out there on the web.

I downloaded a few and modified them, last time I needed to do this.

maxweels

3:20 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Can you give me some example code snippets thank you

maxweels

11:09 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Hello again i have try

this

$tag_by_default=$_SERVER['SCRIPT_NAME'];

and it return this /avril-lavigne-girlfriend/index.php

i want it return like this: avril lavigne girlfriend

thank you

g1smd

11:21 pm on Jul 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You'll need to break it into components, splitting at the "/" and grab just the one that is the folder name.

Then, you'll need a regex to replace hyphens with spaces, and you'll need to Uppercase the first letter of the words.

You'll learn a lot more by playing with the code...

henry0

11:40 am on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi, best could be learning about tagging from a "tagging dedicated tutorial"
use google (USA) and search for
"Tagging-With-PHP-And-MySQL"

FR:
Salut Carlos,
cherche dans Google, US version, pour la phrase comme elle est postee au dessus.

That's it, no more French for the day, let's use English :)

<edit>Typo</edit>

maxweels

1:28 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



thank for your reply henry

maxweels

2:58 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



hi have try this and it's working :

$filename = $_SERVER['SCRIPT_FILENAME'];
$fn = explode('/',$filename);
if(count($fn) >= 2) {
$tag_by_default = str_replace('-',' ',$fn[(count($fn)-2)]);
}
else {
$tag_by_default = str_replace('-',' ',$fn[0]);
}

before it was return this /avril-lavigne-girlfriend/index.php

and now it's like this avril lavigne girlfriend

so it's very good

i have anotther question in /avril-lavigne-girlfriend/index.php

i want it return only : avril lavigne

where is the code to be modified

this maybe : $fn = explode('/',$filename);

Chris_Mohr

8:21 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



It depends how you would like this rule to work.

Do you always want the first two words?

Do you always want to exclude the last word?

You could use preg_match at the end, but writing regular expressions can be a little tricky. If you wanted the if you wanted the first two words you would do something like this:

preg_match('/^([a-z]+\s[a-z]+)/', $tag_by_default, $matches);
$new_tag = $matches[1];

But maybe a better way to do it, is to use explode() instead of str_replace.

Like:

if(count($fn) >= 2) {
$word_array = explode('-', $fn[(count($fn)-2)]);
}
else {
$word_array = explode('-', $fn[0]);
}

$tag_by_default = "$word_array[0] $word_array[1]";

g1smd

8:23 pm on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Take the final data you already have, put it in a new variable, and explode that by spaces, and discard the last one.

You might also want to uppercase the first letter.

maxweels

8:42 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



hi everybody

i have test this code and it's working

if(count($fn) >= 2) {
$word_array = explode('-', $fn[(count($fn)-2)]);
}
else {
$word_array = explode('-', $fn[0]);
}

$tag_by_default = "$word_array[0] $word_array[1]";

maxweels

8:55 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



so finally my code is work like i want :

$filename = $_SERVER['SCRIPT_FILENAME'];

$fn = explode('/',$filename);

if(count($fn) >= 2) {

$site_title = str_replace('-',' ',$fn[(count($fn)-2)]);
}

else {

$site_title = str_replace('-',' ',$fn[0]);
}

$filename = $_SERVER['SCRIPT_FILENAME'];

$fn = explode('/',$filename);

if(count($fn) >= 2) {
$word_array = explode('-', $fn[(count($fn)-2)]);
}
else {
$word_array = explode('-', $fn[0]);
}

$tag_by_default = "$word_array[0] $word_array[1]";

g1smd

9:00 pm on Jul 12, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That looks good. If the text is to be used for anchor text you might want to look at ucfirst and/or ucwords as well.

You've learnt a lot about PHP in this exercise; stuff that you can re-use many times in the future. Hopefully, you're now more confident with this stuff.

maxweels

10:29 pm on Jul 12, 2008 (gmt 0)

10+ Year Member



hi thank you for all

all is work and it's save me lot of time

because now i don't need to edit this file for other folder it's very good

i have last request

in my config file i have this

$site_url="mysite.com/avril-lavigne-girlfriend/";

i try to do something like this to replace the original by

$site_url=".";

it work on first page but after

when i clic on tag it done this problem >> mysite.com/avril-lavigne-girlfriend/tag/video and more i clic to another tag it continue like this
mysite.com/avril-lavigne-girlfriend/tag/video/tag/song....

so it's not good

maybe i forget something

original here :

$site_url="mysite.com/avril-lavigne-girlfriend/";

modified here:

$site_url=".";

if i want to do this it's for the same reason not reedit more 800 file for just change the adress hope you understand my meaning

thank you