Jump to content

Best way to edit submitted text?


dde

Recommended Posts

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 by dde
Link to comment
Share on other sites

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 by dde
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.