dde Posted January 18, 2015 Share Posted January 18, 2015 (edited) I'd like to edit specific parts of a submitted text what is the best way to do this?For example I get the following text:[name country] is very cold this time of year.Because I like the cold I would love to live there [end of line 2]whole lot more text here until[name country] if this text here exists, blabla [but Germany] is blabla [end of line]in some cases some more text here[summary] bla bla The text in the brackets are words that I already know before it has been submitted.Getting the words out has been part of my previous script, using preg_match and put them in variables.I want to put the first portion of the text in a row, if 2nd, 3rd and 4th portion exist, put them in a row too.Any ideas?[edited] I prefer to have these portions cut out and put in a variable.So I end up having a few variables and can later echo that out in rows Edited January 18, 2015 by dde Quote Link to comment https://forums.phpfreaks.com/topic/294023-best-way-to-edit-submitted-text/ Share on other sites More sharing options...
dde Posted January 18, 2015 Author Share Posted January 18, 2015 (edited) So far I've been thinking about using strpos, to find the position to cut. Then use substr to cut it out and put that portion in a variable. Edited January 18, 2015 by dde Quote Link to comment https://forums.phpfreaks.com/topic/294023-best-way-to-edit-submitted-text/#findComment-1503300 Share on other sites More sharing options...
dde Posted January 18, 2015 Author Share Posted January 18, 2015 (edited) I took the substr and strpos method. /* starting position of the text */ $startpos = 0; /* string to find */ $string = "string"; /* count the string length */ $addcount = strlen($string); /* find the position of the string in the user submitted haystack */ $stringpos = strpos($haystack, $string); /* specify the position to cut the haystack */ $endpos = $stringpos + $addcount; /* put the first portion of the haystack in a vriable to be printed out later */ $header = substr($text, $startpos, $endpos); /* I always use var_dump to check for my results */ var_dump($firstPortion); /* Next portion of the haystack to be cut */ $string = "string"; /* count the previous $header string length */ $addcount = strlen($firstPortion); /* find string position for second portion of haystack, starting where the previous cut ended (strlen($firstPortion)). */ $stringpos = strpos($text, $string); $stringpos = $stringpos - $endpos; /* put the second portion in a variable */ $secondPortion = substr($text, $addcount, $stringpos); /* check for results */ var_dump($secondPortion); Works fine, and as expected. I'm however not satisfied with the coding itself. Any ideas for shorter and more cleaner methods? Edited January 18, 2015 by dde Quote Link to comment https://forums.phpfreaks.com/topic/294023-best-way-to-edit-submitted-text/#findComment-1503310 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.