bahaa Posted October 14, 2011 Share Posted October 14, 2011 Hello, How do we avoid cutting the last word when using the Left Or Right function in MYSQL to select a number x of characters from the database? If it is not possible with the mysql, is there any way to do it in PHP ? Bahaa, Link to comment Share on other sites More sharing options...
Antonio Conte Posted October 14, 2011 Share Posted October 14, 2011 Dunno know exactly how these functions work, but I'm guessing they work as arrays. Remember that an array always starts with Zero, not one. If you want the first to the fourth characters, that would be position 0 to 3. (position: 0, 1, 2, 3) Dunno if this helps you anything. Link to comment Share on other sites More sharing options...
Jonathon Posted October 14, 2011 Share Posted October 14, 2011 I think the RIGHT and LEFT calls in MySQL return a string by character length. I don't really know the answer, maybe explode the string returned and return 1 less than the length of elements that way you wouldn't run the risk that the last element is only a partial word...? Link to comment Share on other sites More sharing options...
Josee Posted October 14, 2011 Share Posted October 14, 2011 Have you tried SUBSTRING_INDEX instead? For instance, this: SUBSTRING_INDEX(column_name,' ',-1) would return anything to the left of the first space, starting from the right. I hope this helps, Link to comment Share on other sites More sharing options...
Recommended Posts