Click to See Complete Forum and Search --> : Hard RegEx Limit title by 19 characters add 2 to the end


Markbad311
03-06-2006, 10:15 PM
This is gonna be tuff. I have an existing function in my library that querys the db and generates these links depending on the 'bar' field .

**Problem I am making these results fit in to a one line button on a side naviagtion menu example :: www.eriescene.com (http://www.eriescene.com). the second nav portion is two lines. some of the titles are too long.

**Solution use some kind of regular expression:

1.... starts from the begining of the title and counts in 19 characters

2.... and replaces the next two charcters with .. (two periods).

3.... Forgets about the rest.


Note: Must work on whitespace, letters, numbers, special characters.

Here is the Existing Link Generating PHP/MYSQL CODE

/////////////////////////////////////////////
//Links to single Events///////////////
///////////////////////////////////////////
function sidemenu($connection) {
global $nav_block;
$table_name = "events";
$sql = "SELECT id, bar FROM $table_name WHERE `date` BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 DAY) and DATE_ADD(CURDATE(), INTERVAL 20 DAY) ORDER BY 'bar'";
$result = @mysql_query($sql) or die(mysql_error());
$num = @mysql_num_rows($result);
if ($num < 1) {
$nav_block = "<p>I am sorry there is no results</p>";
} else {
$nav_block .= "<ul class=\"menu\">";
//if results are found loop through them and make a form selection block list.
while ($row = mysql_fetch_array($result)) {
$id = $row['id'];
$bar = $row['bar'];
$nav_block .= "<li><a href=\"http://www.eriescene.com/index.php?id=$id\">$bar</a></li>";
}
$nav_block .= "</ul>";
}
}


$bar is the variable we'll need to modify. something like

$bar = $preg_replace('findit, limit it and add two period chacters, $bar);


Try as I may stinkin regex's get me everytime. Thank you to anyone that can help me... As I can't get it right and it is way to complicated for my skill level :-) Thanks again

NogDog
03-06-2006, 11:31 PM
Would probably be simpler with substr():

$text = "This is a test. It is only a test. This is the end of the test.";
$shortVers = trim(substr($text, 0, 19)) . "...";
echo $shortVers;