Jump to content


Photo

RegEx limiting result lines by 21 characters


  • Please log in to reply
5 replies to this topic

#1 MarkBad311

MarkBad311
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 07 March 2006 - 04:13 AM

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
[!--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--]


[!--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

#2 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 07 March 2006 - 04:28 AM

[!--quoteo(post=352387:date=Mar 6 2006, 11:13 PM:name=MarkBad311)--][div class=\'quotetop\']QUOTE(MarkBad311 @ Mar 6 2006, 11:13 PM) View Post[/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?

$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 .= '..';

Won't that work?
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#3 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 07 March 2006 - 04:41 AM

Talk about anti-climactic. I had my regex cookbook out and everything.

#4 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 07 March 2006 - 02:04 PM

[!--quoteo(post=352396:date=Mar 6 2006, 11:41 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 6 2006, 11:41 PM) View Post[/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...
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]

#5 wickning1

wickning1
  • Members
  • PipPipPip
  • Advanced Member
  • 405 posts

Posted 07 March 2006 - 09:55 PM

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.

#6 XenoPhage

XenoPhage
  • Members
  • PipPipPip
  • Advanced Member
  • 99 posts

Posted 08 March 2006 - 01:39 PM

[!--quoteo(post=352635:date=Mar 7 2006, 04:55 PM:name=wickning1)--][div class=\'quotetop\']QUOTE(wickning1 @ Mar 7 2006, 04:55 PM) View Post[/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...
--
[a href=\"http://blog.godshell.com\" target=\"_blank\"]XenoPhage[/a]
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Something mysterious is formed, born in the silent void. Waiting alone and unmoving, it is at once still and yet in constant motion. It is the source of all programs. I do not know its name, so I will call it the Tao of Programming.[/quote]




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users