andresc2 Posted June 2, 2011 Share Posted June 2, 2011 hi, how is everybody doing? i hope well, i have a problem with some php code. i am trying to split some text in two parts once they have been split, they are display, but sometimes the php code break the source code of the page such as images and instead of the image i get the source code like this add-plugins-paintnet-1.2-800x800 3857cff9733e6a.jpg" style="border-width: 1px; border-color: #000000; border-style: solid; margin: 4px auto 5px; display: block;" /> i have tried for about 2 days now to fix it and i haven't been able to, so any help or guidence would be greatly appreciated. this is the code that i am currently using, i have tried to change the variable in the $findTag but nothing seems to work <? php $article =$this->article_>text; $countchars=strlen($article); $divide=$countchars/2; $firstpart=substr($article,0,$divide); $secondpart=substr($article,$divide); //i keep changing the <p /> variable to <img /> and $image and many others but nothing seems to work $findTag = strpos($secondpart, "<p /> "); $var = $divide + $findTag $firstpart = substr($article, 0, $var); $secondpart = substr($article, $var); ?> thanks Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/ Share on other sites More sharing options...
mikesta707 Posted June 2, 2011 Share Posted June 2, 2011 The purpose of what this code is doing is kind of unclear, but the reason you have splitting at unfortunate places is because of how you position the split. You basically split the string at an arbitrary and meaningless position. You use $var, which is the arbitrary middle of the string (which you may want to cast as an int btw, as you will have decimal values for strings with an odd length) added to the position of the "</p>" tag. I'm not sure why you do this, but depending on the value of article (and if it will have a value that is expected, or a dynamic calue from a database based on user input or something) you could easily have the string split at inconvenient places. What exactly are you trying to accomplish here? Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1223886 Share on other sites More sharing options...
WebStyles Posted June 2, 2011 Share Posted June 2, 2011 I also don't really get what is intended. Can you post an example of the content in $article and maybe also a printscreen of what you would like the final result to look like? thanks Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1223994 Share on other sites More sharing options...
teynon Posted June 2, 2011 Share Posted June 2, 2011 The other two or right, need more information. But your script is showing the content because your substr's are erasing the <img src=" tag before an image. You're going to have to get more involved to either remove image tags, crop the text, then reinsert image tags, or just take images out altogether. My guess is you are trying to make a featured article type thing that only shows a portion of the whole listing. If that's the case, I would remove images altogether. You're getting into an area that becomes very complicated though, as you'll have to determine if any HTML tags are open or not. The easiest way is to pull all HTML tags out of the content using a regular expression. Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1224011 Share on other sites More sharing options...
andresc2 Posted June 2, 2011 Author Share Posted June 2, 2011 hi sorry for all of the confusion, what i am trying to do is to insert a banner in the middle of the article. so i am splitting the article into two parts but sometimes it splits the source code of an image and it messes with how the article looks. i have attach an image of how it looks. so what i am trying to accomplish with the $findtag variable is to find where the image starts and where it ends and add it to to the first part so that the image does not split into two parts this is my current code: <? php $article =$this->article_>text; $countchars=strlen($article); $divide=$countchars/2; $firstpart=substr($article,0,$divide); $secondpart=substr($article,$divide); $findTag = strpos($secondpart, '~<img [^>]* />~'); $var = $divide + $findTag $firstpart = substr($article, 0, $var); $secondpart = substr($article, $var); ?> and every time i have a different image. [attachment deleted by admin] Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1224039 Share on other sites More sharing options...
teynon Posted June 2, 2011 Share Posted June 2, 2011 I wont make the code for you, but what you could do is this: split the code. Section 1, section 2. Then, count the number of html opening tags (<blah) using preg_match. Then, count the number of closing tags (>). If they are unequal. Then, find the last < start tag in section 1. Get the strpos to that tag and trim it off and put it on section 2. Then insert your image. Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1224053 Share on other sites More sharing options...
andresc2 Posted June 3, 2011 Author Share Posted June 3, 2011 ok Thanks i give it a try and see what i come up with. Quote Link to comment https://forums.phpfreaks.com/topic/238164-how-to-skip-over-images-when-splitting-text/#findComment-1224654 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.