Forum Moderators: coopster

Message Too Old, No Replies

Remove articles (a, an, the) from the beginning of a string.

         

Ann_G

10:36 pm on Mar 24, 2005 (gmt 0)

10+ Year Member



Sorry if I overlooked it, but I'm having a hard time finding info on how to remove articles (a, an, the) from the beginning of a string. I have created a booklist database. If the titles start with (a, an, the)I want to be able have them show up with the respective article, except I want them to be alphabetized according to the second word in the title. How do I write a PHP code that ignores the articles. Right now I just don't enter the first word, if it's an article and then this works fine:

$query = "SELECT * FROM mysteries ORDER by title ASC";

$result = mysql_query ($query); // Run the query.

while ($row = mysql_fetch_array($result)) {

$md_id = $row['book_id'];
$title = stripslashes($row['title']);
$author = stripslashes($row['author']);

Thankful for a suggestion.

dreamcatcher

9:05 am on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ann,

For titles beginning with 'The', why not list them like this:

Philospher`s Stone, The
Goblet of Fire, The

As for stripping characters, easiest way to display them alphabetically, would be to create a field in your database to get the first letter when you process. This will make it easier when displaying your data. So, a char(1) field should do and when you add to your database do the following:

$booktitle = $_POST['booktitle'];
$actualtitle = $booktitle; //Make a Copy and insert this as the title

$booktitle = str_replace("An ", "", $booktitle);
$booktitle = str_replace("A ", "", $booktitle);
$booktitle = str_replace("The ", "", $booktitle);

//Then use substr to get the first character:

$character = substr($booktitle,0,1);

Insert this character into your new field and then you can order by this when you pull your data.

Think that should work ok.

dc

Ann_G

6:02 pm on Mar 25, 2005 (gmt 0)

10+ Year Member



Thanks a lot. I'll try it as soon as I have a chance. I'll let you know how it worked.

Ann_G

6:59 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



I tried what you suggested DC but can't get it to work the right way. The char(1) field collects the first letter of the title, which means the letter t if it's the article "the", letter a if article is "a". The str_replace() function doesn't appear to be working. How can I collect the first letter in the title after those articles are stripped so that I then can SELECT and sort by that first letter. I just can't figure it out though I fiddled around all morning with this. Any idea what I could be overlooking. My script look like this. The form field is: name="title"

$title = $_POST['title'];

$title = str_replace("An ", "", $title);
$title = str_replace("A ", "", $title);
$title = str_replace("The ", "", $title);

$actualtitle = $title; //The title to INSERT

//Substr to get the first letter:

$first_letter = substr($title,0,1);

// Check for an author's last name.
$author = trim($_POST['author']);
if (empty($author)) {
$author = '';
}

if (isset($_POST['submit'])){

$query = "INSERT INTO trial (first_letter, actualtitle, author) VALUES ('$first_letter','$actualtitle','$author')";

// Run the query.
$result = @mysql_query ($query);
}

?>

dreamcatcher

8:06 pm on Mar 28, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Ann,

It might be a case sensitive issue. Try juggling the code around:


$title = $_POST['title'];

$actualtitle = $title; //The title to INSERT

$title = str_replace("an ", "", strtolower($title));
$title = str_replace("a ", "", strtolower($title));
$title = str_replace("the ", "", strtolower($title));

//Substr to get the first letter:

$first_letter = substr($title,0,1);

I changed the case to lower case for string replace, then forced lower case on the data by using strtolower.

dc

Ann_G

10:41 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Yes, it was case sensitive. Now it works perfectly when I add it to the table, as I can see when I look in MySQL. I thought it would be simple to get it to do the reverse when I use the SELECT query to view it in a browser. I didn't want to ask because it seemed to me I should be able to figure it out. But here i go again (nothing makes me feel as stupid as PHP scripts) -- any idea what to do? This is what I have right now though I tried other things as well. I want the title to come back with the repective articles the, an, a

// Declare a title.
$actualtitle = ($_POST['actualtitle']);
$title = ($_POST['title']);

$actualtitle = $title;

//:

$title = strtr("an ", "",($title));
$title = strtr("a ", "", ($title));
$title = strtr("the ", "",($title));

$query = "SELECT * FROM trial ORDER by first_letter ASC";

$result = mysql_query ($query); // Run the query.

while ($row = mysql_fetch_array($result)) {

$id = $row['id'];
$first_letter = stripslashes($row['first_letter']);
$actualtitle = stripslashes($row['actualtitle']);
$author = stripslashes($row['author']);

$display_block .= "<p><strong>$actualtitle </strong><br />
$author
</p>";
}

bobzimuta

5:01 pm on Apr 21, 2005 (gmt 0)



Rather than using a replace method for every possible title beginning ('a', 'an', 'the') the perl regular expression replace (preg_replace) method will do it for you in one swoop.

$shortened_title = preg_replace("/^(the¦a¦an)\b\ */i", "", "Some title");

This is case-insensitive and will only look for the first word in the title (i.e. it will not remove a 'the' halfway through the title). You can also add new words to replace within the function, insert anything in the section


the¦a¦an

jatar_k

5:23 pm on Apr 22, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld bobzimuta