Jump to content

how to skip over images when splitting text


andresc2

Recommended Posts

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

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.