rhock_95 Posted October 28, 2007 Share Posted October 28, 2007 lets say I have a block of text... I want to echo a single sentence from about the middle of the string what is the easiest way to do this? to clarify...the "block of text" is the $result of curl script TIA Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/ Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 substr(); Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379542 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 can you digress a bit on using substr() can you show or link me to an example of getting a line of text from the middle of a string? Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379543 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 <?php function mid_string($string,$len){ $count = strlen($string); $half = $count*.5; $half--; return substr($string,$half,$len); } ?> That will return a string of length you define in calling it from the string inputted started at the exact half way through Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379546 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 ok thanks... but I need to specify a point in the string to start and stop what gets printed... how do I define the the beginning and the end points? can it be done using character counts? or something? Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379549 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 substr doesn't have an end point deffinition, only a start and length fyi everything I'm saying is found http://us.php.net/manual/en/function.substr.php and its saves us both time if u use that if you know the general idea of what you need. Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379550 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 believe me I have been through the manual but not be a fluent php coder I am lost... what does the "start" integer define ? is it the number of characters or what? Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379554 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 start is the position of the first character to grab. in the string $string = "fishing"; 0 = f 1 = i g =-1 you can use negatives to go back wards so if i did substr($string,0,2); i get fi or substr($string,-1,2); gives gn Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379556 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 ok great...are white spaces ignored? Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379562 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 nope, read up on php.net cause you won't get very far with php if you can't read what it says there. a string type variable consist of a series of characters that can be manipulated by the string function library and used in a lot of applications. all characters in it are relative for a comparasion/function use. Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379563 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 ok thanks for your help and insights...one more question... what is the easiest way to get the character count of any given string? thanks again Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379564 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 i wrote this above strlen Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379565 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 can you show me an example? can you show me the syntax how to strip tags then echo a substr ? Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379570 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 no read it on php.net I'm not here to give u a sumary of what is already there. Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379571 Share on other sites More sharing options...
rhock_95 Posted October 28, 2007 Author Share Posted October 28, 2007 thanks for nothing...obviously you as a php coder you have no idea how hard it is trying to grasp something from the manual without hands on experience... telling someon to go read the manual is about the lamest advice that anyone can give... effing worthless if anyone else can help me I would much oblighed...I am not asking anyone to write a script I am just asking for some hands on syntax examples that the manuals fail top provide... Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379575 Share on other sites More sharing options...
tidalik Posted October 28, 2007 Share Posted October 28, 2007 ::)IF you read the manual you would see they give TONS of examples. PHP.net is one of the best online manuals, it makes sense, low on the jargon and gives you lots of working examples. Type the suggestions into the function search at php.net Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379583 Share on other sites More sharing options...
cooldude832 Posted October 28, 2007 Share Posted October 28, 2007 if it sounds like I am being hard on you it is because I am. If you don't learn now how to solve your own problems there is no way you will ever learn. Now if you went into php.net and looked up strlen and ask a question like "What does it return" I'd say an integer. but asking what it does/how to use it is pointless if you don't try it on your own/look it up on your own. Quote Link to comment https://forums.phpfreaks.com/topic/75053-echo-only-select-text-from-string/#findComment-379589 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.