Jump to content

separate text from the image into two different variables


ebchost

Recommended Posts

For example

<img src="http://www.vesti-online.com/data/images/2011-06-05/156013_sampioni_kf.jpg?ver=1307647416"><br/>Upravo zavrÅ¡ena klupska sezona obeležena je potpunom dominacijom Partizana, s obzirom da su sekcije naÅ¡eg najbrojnijeg sportskog druÅ¡tva osvojile Äak 11 pehara u pet najpopularnijih kolektivnih sportova.<br/><br/>

 

this is part of som news article wich i have in my database in tabele.

 

How i can to separete imege url from rest part of text in 2 diferent variable

 

$image_url = " url image value from item";

$text = "rest of text value";

 

in this example final resolt wil be

 

$image_url = "http://www.vesti-online.com/data/images/2011-06-05/156013_sampioni_kf.jpg";

$text = "Upravo zavrÅ¡ena klupska sezona obeležena je potpunom dominacijom Partizana, s obzirom da su sekcije naÅ¡eg najbrojnijeg sportskog druÅ¡tva osvojile Äak 11 pehara u pet najpopularnijih kolektivnih sportova.";

 

 

thanks.

Link to comment
Share on other sites

You'll find that there are various ways to do this. To isolate the URL, I am going to use a function called preg_match() which uses a regular expression to match substrings to a pattern.

$pattern = '/\"(.?)\"/';
preg_match($pattern, $subject, $matches)

Where $subject will be the string with your img tag.

To find the text after the URL, we will be doing the same thing, simply changing the search pattern a bit.

$pattern = '/<br\/>(.?)<br\/>/i';

Simply use that pattern in the preg_replace() to find the  string of text. $matches[0] will be the result that you want

Link to comment
Share on other sites

$podaci = mysql_query("SELECT * FROM rss_vesti") or die(mysql_error());

		while($row = mysql_fetch_assoc( $podaci ))
			{
				$naslov = $row['NASLOV'];
				$sadrzaj = $row['SADRZAJ'];
				$izvor = $row['IZVOR'];
				$datum = $row['DATUM'];

				?>
				<table>
				<tr>
				<td>

				<?php 

				preg_match_all('/<ul>(.*?)<\/ul>/i', $sadrzaj, $matches);
				echo $matches[0][0]; ?>

				</td>
				</tr>
				</table>
				<?php

			}

 

i hava emty screen, no resolts?

 

But, in your original code, resolt egsisting.

Link to comment
Share on other sites

yes, make the variable the src of your images, do not wrap it in single quotes, if you dont want to do it that way, you can use str_replace() on the matches of the pre_match() code that i gave you earlier, like so

$matches = str_replace($matches, '"', '');

Link to comment
Share on other sites

$podaci = mysql_query("SELECT * FROM rss_vesti") or die(mysql_error());

		while($row = mysql_fetch_assoc( $podaci ))
			{
				$naslov = $row['NASLOV'];
				$sadrzaj = $row['SADRZAJ'];
				$izvor = $row['IZVOR'];
				$datum = $row['DATUM'];

				?>
				<table>
				<tr>
				<td>

				<?php 
					$garbage = 'just some bs <ul><li>text</li><li>text</li><li>text</li></ul>just some more bs';
					$test = preg_match_all('/\"(.*?)\"/', $sadrzaj, $matches);
					echo $matches[0][0];
					$slika = str_replace($matches, '"', '');
					echo $slika;
					echo "<img src='$slika' alt='' height='100' width='100'/>";

				?>

				</td>
				</tr>
				</table>
				<?php

			}

 

This code i use in this moment

 

resolt can be check it in http://www.moravaprom.com/ebchost/prikaz.php adres

Link to comment
Share on other sites

<?php 
					$garbage = 'just some bs <ul><li>text</li><li>text</li><li>text</li></ul>just some more bs';
					$test = preg_match_all('/\"(.*?)\"/', $sadrzaj, $matches);
					echo $matches[0][0];
					$slika = str_replace($matches[0][0], '"', ''); //need to add distinct positioning
					echo $slika;
					echo "<img src='$slika' alt='' height='100' width='100'/>";

				?>

Link to comment
Share on other sites

i think we (i) need update for this problem

 

How to correct this (upgrade)?

 

for some image, i have url value like this

 

$image = "width: 180px; float: right; margin:3px; text-align:center;"

 

you can check it hiar: http://moravaprom.com/ebchost/index.php

(as you can see, i make progres in my work, but i still have litle off beginig problem)

 

thanks in forvard :)

Link to comment
Share on other sites

Can you pinpoint your problem for us please

 

this is code for echo rss value

 

$items = $rss_link->channel->item;

				foreach($items as $item)
					{
						$title 			= $item->title;
						echo "<h2>LOGO:</h2>$title</br>";
						$link 			= $item->link;
						echo "<h2>LINK :</h2>$link</br>";
						$published_on 		= $item->pubDate;
						echo "<h2>Published_on:</h2> $published_on</br>";

						$description 		= $item->description;
						$descritpion_strip	= strip_tags($description);
						$description_trim 	= trim($descritpion_strip);
						echo "<h2>description_trim:</h2> $description_trim</br><hr>";

						$image_object = preg_match_all('/\"(.*?)\"/', $description, $matches);
						$image = str_replace( '"','',$matches[0][0]);


                           			}

 

this part of code colecting url of image

$image_object = preg_match_all('/\"(.*?)\"/', $description, $matches);
$image = str_replace( '"','',$matches[0][0]);

 

and sometimes, show bad value

for example:

$image = "width: 180px; float: right; margin:3px; text-align:center;"

 

How can I avoid this and get the correct value image url?

 

thanks

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.