RobertP Posted October 2, 2011 Share Posted October 2, 2011 hello everyone ... i am here to see if you can help me with a small issue. i am creating a search feature on my cms, and when someone searches for something, i want to cut the text... i don;t know how to explain it very well, but i will give an example. Searching for 'text' ... 3 results found. original text (987654321.text.123456789) cut text (4321.text.1234) i have been using substr, but it is not working out for me. ill show what i have been using. if(strstr($page_content,$q)){ $strPos = strpos($page_content,$q); $search_results[] = str_replace($q,'<span style="background-color:#FFFF00;color:#333333;">'.$q.'</span>',substr($page_content,$strPos-10,-10)); } Quote Link to comment https://forums.phpfreaks.com/topic/248274-search-feature/ Share on other sites More sharing options...
SparK_BR Posted October 3, 2011 Share Posted October 3, 2011 if(strstr($page_content,$q)){ $strPos = strpos($page_content,$q); $search_results[] = str_replace($q,'<span style="background-color:#FFFF00;color:#333333;">'.$q.'</span>',substr($page_content,$strPos-10,-10)); } shouldn't your substr be using 10 as third parameter instead of -10? actually, you should use substr($page_content,$strPos-10,strlen($q)+10); or something if you want to cut 10 before and 10 after the keyword and not counting with it Quote Link to comment https://forums.phpfreaks.com/topic/248274-search-feature/#findComment-1275245 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.