JasonTTex84 Posted October 26, 2011 Share Posted October 26, 2011 I have this command that basically populates my website with titles for pages based on the information that is placed within "info.txt" I have multiple titles being created so I'd like a way of putting them in order beside alphabetically. My thought was that I could place numbers as the first character in the "info.txt" file and that would allow me to put the titles in an order I find acceptable. Now this causes the issue that I don't want those numbers displayed on my website. So the Echo needs to strip the number.... I'm lost, but my brain thinks it's a good idea. Here's the code I'm working with: <tr> <td id="body"> <table id="columns" cellspacing="0"> <tr> <td> <h2>LSAV Latest Events</h2> <p>Select an event from below to view a gallery of images from that event.</p> <ul> <?php $dir = 'latest-events'; if ($handle = opendir($dir)) { while (false !== ($file = readdir($handle))) { if( $file != '.' && $file != '..') { $title = file_get_contents( "$dir/$file/info.txt" ); echo "<li><a href=\"event-viewer.php?event=$file\">$title</a></li>"; } } closedir($handle); Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/ Share on other sites More sharing options...
jotorres1 Posted October 26, 2011 Share Posted October 26, 2011 Try this: <?php $len = strlen($title); $title = substr($title, 1, $len); ?> Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282534 Share on other sites More sharing options...
markjoe Posted October 26, 2011 Share Posted October 26, 2011 length is optional on substr() substr($title, 1); Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282535 Share on other sites More sharing options...
JasonTTex84 Posted October 26, 2011 Author Share Posted October 26, 2011 Try this: <?php $len = strlen($title); $title = substr($title, 1, $len); ?> Should I insert this before or after the Echo line? Thanks for the quick replies here as well!! Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282536 Share on other sites More sharing options...
jotorres1 Posted October 26, 2011 Share Posted October 26, 2011 Before, and also, just like markjoe said, $len is optional. I just added it in there by default habits. Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282537 Share on other sites More sharing options...
markjoe Posted October 26, 2011 Share Posted October 26, 2011 you could actually do it inline ... echo "<li><a href=\"event-viewer.php?event=$file\">" . substr($title, 1) . "</a></li>"; ... Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282538 Share on other sites More sharing options...
JasonTTex84 Posted October 26, 2011 Author Share Posted October 26, 2011 you could actually do it inline ... echo "<li><a href=\"event-viewer.php?event=$file\">" . substr($title, 1) . "</a></li>"; ... This code worked great in the fact that it did indeed strip the first letter from my text file, however I can't figure out what is determining the order in which these Titles are displayed. I tried put 1-2-3-4 in front of each title string as well as a-b-c-d and neither worked for me. Any suggestions?? Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282543 Share on other sites More sharing options...
PFMaBiSmAd Posted October 26, 2011 Share Posted October 26, 2011 You should use a specific separator character that will never exist in the data, such as a |, between the 'fields' in your data. You would then explode each line using that separator character. What happens with the current code once you get more than 9 events saved in the file? Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282547 Share on other sites More sharing options...
JasonTTex84 Posted October 27, 2011 Author Share Posted October 27, 2011 You should use a specific separator character that will never exist in the data, such as a |, between the 'fields' in your data. You would then explode each line using that separator character. What happens with the current code once you get more than 9 events saved in the file? I get what your saying... Perhaps use letters, a-z = 26... I'll never have more than say 10-15 events listed. I guess my issue right now is that I don't understand what method the PHP is using to sort the items I have now, everything is in alphabetical order off of the first letter of the Title (of course ignoring the letter that I put in front of it i.e. a-z...) except for the last two which start with "Z" and "Q" and are listed in that order. None the less I don't want alpha order I really want to determine the order myself. What I have is this, I have a directory with folders, each folder has photos as well as thumbs of each photo as well as a file named "info.txt". Whatever I type in the "info.txt" file is what shows up on the page as the title of the page containing the photos in that directorty. So each "info.txt" file basically has a few words in it with a year and a couple "-". That's all. When I started this thread all I wanted to accomplish was setting the order in which these titles were displayed, I thought I was on the right track but obviously not. Thanks for all of your help thus far, you have given me exactly what I've asked for... Just seems that what I'm asking for isn't the solution.... Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282576 Share on other sites More sharing options...
JasonTTex84 Posted October 27, 2011 Author Share Posted October 27, 2011 Well, I fixed my issue this morning... It seems that PHP was using the first capital letter to use for sorting purposes. I am all good to go . Thanks for everyone's help. I'm sure you'll see more from me JT Quote Link to comment https://forums.phpfreaks.com/topic/249875-new-to-php-need-to-strip-first-character-from-a-string/#findComment-1282661 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.