Copy link to clipboard
Copied
For every post in this section I have about 7-10 related images. I fill out a description using a wysiwig editor to insert into the database. Each post usually has about 5 paragraphs about the article, followed by <h2> with paragraphs about technical specs. What I want to do is be able to echo the images specifically.
1- Image before the first paragraph
3- After the third paragraph
1- After the <h2>
2-5 next to the paragraphs of the technical specs.
My question is one, can I acheive this, and two where should I start reading about how to acheive this. I beleive I will use regex but have read upon other methods and know this will be a mix of php/css. But don't know too much from there, as this is more complicated than what I do know.
If anyone can link me to some articles/tutorials or books that explain in great detail I would appreciate it!
Thanks!
Copy link to clipboard
Copied
Are you using <p> tags for your paragraphs? If so, you can probably just use a few php string functions to insert your image tags.
Copy link to clipboard
Copied
Yes, so im using the CKEditor, I start a new paragraph by pressing enter which formats the <p> tags. I can change a heading say after the story to whatever heading I want ex. <h2> and after that heading I have areas of technical specs. Similar to below. That is what is inserted to the db, but of course I just echo the 'description' which displays it. So I want to be able to count <p> and <h2> tags and be able to insert the images specifically.
<p>Text paragraph 1 </p>
<p>Text paragraph 2</p>
<p>Text paragraph 3</p>
<p>Text paragraph 4</p>
<p>Text paragraph 5</p>
<h2>New heading</h2>
<h3>Another heading</h3>
<p>Text paragraph</p>
<h3>Another heading</h3>
<p>Text paragraph</p>
<h3>Another heading</h3>
<p>Text paragraph</p>
<h3>Another heading</h3>
<p>Text paragraph</p>
Thanks
Copy link to clipboard
Copied
I'm not a php developer, so I don't know the most efficient method - but I would probably use strpos()to locate all of the occurances of the tags of interest. Or use something like this:
http://www.phpro.org/examples/Find-Position-Of-Nth-Occurrence-Of-String.html
Then use substr_replace() to insert your image tags.
Copy link to clipboard
Copied
Yes that looks very interesting. The problem that I may encounter is a variation in <p> tags between new posts. Some may have 3 and some may have 6 or 7. But I think I can come up with a method. Thanks for the good article. I will search more about strpos() and subtr_replace(). I have read some of strchr -to get all text before first occurance of _ .
I will do more reading, and if I come up with a accurate method will post here.
Copy link to clipboard
Copied
Ok so I have come to a problem using the str_replace.
What I have is in my description field, and I am now putting placeholders for certain images. Ex. <image1> and what I want to do is replace the <image1> with a defined image.
So what it looks like now for the images is the following and what I would need to use to insert in the replace variable for str_replace.
<?php foreach ($pix1 as $picture) { ?> | ||
<p> | ||
<a class="fancybox" href="images/<?php echo $picture['filename']; ?>" data-fancybox-group="gallery"><img src="images/<?php echo $picture['filename']; ?>" alt="" /></a> | ||
</p> | ||
<?php } ?> |
I want to be able to replace it with the class fields so I can use the fancybox for images on the page.
So my question is how can I use the fucntion I created as the "replace variable" using str_replace.
Copy link to clipboard
Copied
Thanks for reasonable solution. I will be using similar approach as I was looking for it too and see how it goes and will share how it goes.
Copy link to clipboard
Copied
Ok so I have been making some progress on this and have the following bellow.
<?php foreach ($pix1 as $picture) { ?>
<?php
$image = $picture['filename'];
$image_path = '<img src="images/.$image"/>';
$rawstring = $featured_article['description'];
$new_string = $rawstring;
$new_string = str_replace("[image1]", $image_path, $new_string);
echo $new_string;
?>
<?php } ?>
The problem is that when I echo the new string the "picture" is just a red X. Which seems I have been making some progress but not fully built yet...
Any input on this would be great. Thanks.
Copy link to clipboard
Copied
Ok figured it out! Simple mistake (thankfully) but took time to figure out (still learning). Here is the code working. The part in bold is what I was missing...
<?php foreach ($pix1 as $picture) { ?>
<?php
$image = $picture['filename'];
$image_path = 'content_management/image_upload/';
$image_location = '<img src="images/' . $image . '" />';
$rawstring = $featured_article['description'];
$new_string = $rawstring;
$new_string = str_replace("[image1]", $image_location, $new_string);
echo $new_string;
?>
<?php } ?>