Forum Moderators: coopster

Message Too Old, No Replies

can someone help me with this script

         

indiandomain

2:43 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



i have a list of terms

loans
home loans
car loans
.
.

i want the script to output the list in the following format

loans, loans.php
home loans, home-loans.php
car loans, car-loans.php

i tried using javascript but i cant get to input the - in the files.

this should be easy for any php guru here.

i would appreciate any help.

coopster

2:56 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



One option would be to use preg_replace [php.net] to locate any whitespace and replace it with a single hyphen character (-):

<?php
$list = array('loans', 'home loans', 'car loans');
foreach ($list as $value) {
print $value . ', ' . preg_replace("/\s+/", '-', $value) . '.php<br />';
}
?>

indiandomain

3:28 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



coopster
anyway i can input the list?
ur script works but ill have to input the terms manually in the script.

i wanted the script to pickup the list as input the show the output

coopster

3:36 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I loaded an array with your terms as an example. Where is your terms list coming from?

jetboy_70

3:47 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



Create a form with a textarea element in it. Call it 'keywords'.

<textarea name="keywords" rows="6" wrap="off"></textarea>

Paste your list of keywords into the textarea. Post the form to your script. Access the form field and split the different keywords into array elements using:

$list = explode("\r\n", $_POST['keywords']);

Use $list in Coopster's function. :)

ergophobe

6:08 pm on Dec 8, 2003 (gmt 0)

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



If you are certain that you are only replacing spaces, you should use str_replace instead of preg_replace, for efficiency's sake (faster). As the manual says under str_replace


If you don't need fancy replacing rules, you should always use this function instead of ereg_replace() or preg_replace().

so...

str_replace(" ", "-", $value);

Tom

coopster

6:29 pm on Dec 8, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



True, but as ergophobe said, only if you are replacing a single space. Using str_replace(), any double spaces entered are going to return double dashes as well:

print str_replace(" ", "-", 'car loan'); // prints car-loan
print str_replace(" ", "-", 'car loan'); // prints car--loan

If you want to replace any whitespace with a single dash, you'll need the regular expression.

indiandomain

9:14 pm on Dec 8, 2003 (gmt 0)

10+ Year Member



thanks a millio guys.
i used an input table to enter the list.
it worked well.

appreciate all the help.

:)

ergophobe

10:28 pm on Dec 8, 2003 (gmt 0)

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



Good point Coopster. I should have thought of it.

I'm a text editor by profession and can attest that even very clean text often has a scattering of double spaces throughout.

Tom