Jump to content

RegEx limiting result lines by 21 characters


MarkBad311

Recommended Posts

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 :: [a href=\"http://www.eriescene.com\" target=\"_blank\"]www.eriescene.com[/a]. 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.


[!--coloro:#FF9900--][span style=\"color:#FF9900\"][!--/coloro--]Note: Must work on whitespace, letters, numbers, special characters.[!--colorc--][/span][!--/colorc--]

Here is the Existing Link Generating PHP/MYSQL CODE
[b][!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]
/////////////////////////////////////////////
//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>";
}
}
[!--colorc--][/span][!--/colorc--][/b]

[!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]$bar[!--colorc--][/span][!--/colorc--] is the variable we'll need to modify. something like

[!--coloro:#009900--][span style=\"color:#009900\"][!--/coloro--]$bar = $preg_replace('findit, limit it and add two period chacters, $bar);[!--colorc--][/span][!--/colorc--]


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
[!--quoteo(post=352387:date=Mar 6 2006, 11:13 PM:name=MarkBad311)--][div class=\'quotetop\']QUOTE(MarkBad311 @ Mar 6 2006, 11:13 PM) [snapback]352387[/snapback][/div][div class=\'quotemain\'][!--quotec--]
**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.
[!--coloro:#FF9900--][span style=\"color:#FF9900\"][!--/coloro--]Note: Must work on whitespace, letters, numbers, special characters.[!--colorc--][/span][!--/colorc--]
[/quote]

Why regex?

[code]
$rls = "This is my really really long string with way to many characters that needs to be chopped up and have two little periods added to the end";
$newstr = substr($rls, 0, 19);
$newstr .= '..';
[/code]

Won't that work?
[!--quoteo(post=352396:date=Mar 6 2006, 11:41 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 6 2006, 11:41 PM) [snapback]352396[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Talk about anti-climactic. I had my regex cookbook out and everything.
[/quote]

Ooh.. cookbook.. *drool*

Which one? I could use a good cookbook... I took the regex stuff from one of the O'rielly perl books and put together a 2 page regex chart that's come in handy, but a cookbook would be nice...
[!--quoteo(post=352635:date=Mar 7 2006, 04:55 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 7 2006, 04:55 PM) [snapback]352635[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Heh, sorry, I was just kidding. :) I used the Perl Cookbook and Programming Perl when I was learning regular expressions, but these days I have no need for such things.
[/quote]

Bummer.. I'd like to get my hands on a good reference guide... Something like a pocket guide to regex...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.