RE: Using strlen and substr to Shorten DB Results

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> I have a script that I am working on that pulls info from a database:
> 
> <-- Code -->
> $getscname = mysql_query("SELECT * FROM subcat WHERE subcatid =
> '$subcatid'") or die ("Couldn't get the info.<br>".mysql_error());
> 
> 	while($sub_cat = mysql_fetch_array($getscname)){
> 		$subcat_id = $sub_cat['subcatid'];
> 		$subcat_name = $sub_cat['subcatname'];
> 
> 		echo '<a
>
href="subcat.php?catid='.$catid.'&sc='.$subcat_id.'">'.$subcat_name.'</a
> >&nbsp;&nbsp;&nbsp;&nbsp;|&nbsp;&nbsp;&nbsp;&nbsp;';
> 	}
> <-- Code -->
> 
> At the end of each link, a pipe is printed. I want to take the pipe
off
> of the last result, so that the page will prit out:
> 
> SubCat1  |  SubCat2  |  SubCat3
> 
> I've tried using strlen & substr to do this, but it cuts the pipe off
of
> all the results, not the last one. Any suggestions?

Based on how you're building your string, you'll want to remove the last
25 characters from it to get rid of the last | and all of the &nbsp;
characters. So, this should work:

$fixed_str = substr($old_str,0,-25);

Or, you could use a regular expression to match the last | and whatever
follows it up to the end of the string.

$fixed_str = preg_replace("/|[^|]*$/","",$old_str);

---John W. Holmes...

PHP Architect - A monthly magazine for PHP Professionals. Get your copy
today. http://www.phparch.com/



-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux