0

I am accessing a mysql database and displaying a column. in this column is a long string lets say its this:

<image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5">
  <file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file>
  <title/>
  <link/>

whatever lies between <![CDATA[images/ and .jpg]]></file> will change on every row and i want to echo whatever lies between them pieces of code.

anyone help?

thanks

edit

so far i have:

function inStr ($needle, $haystack)
{
  $needlechars = strlen($needle); //gets the number of characters in our needle
  $i = 0;
  for($i=0; $i < strlen($haystack); $i++) //creates a loop for the number of characters in our haystack
  {
    if(substr($haystack, $i, $needlechars) == $needle) //checks to see if the needle is in this segment of the haystack
    {
      return TRUE; //if it is return true
    }
  }
  return FALSE; //if not, return false
}  
$img = '
SELECT *
FROM `item`
';
$result0 = mysql_query($img);

while ($row0 = mysql_fetch_array($result0)){

$haystack = $row0['elements'];
$needle = '<![CDATA[images/';
}

if(inStr($needle, $haystack))
{
  echo "string is present";
}  
7
  • if the stuff that wraps around what you are searching for is consistent, substr with strpos is probably better. Commented Jun 11, 2011 at 17:13
  • please can you give an example, i havnt used strpos before. Commented Jun 11, 2011 at 17:14
  • I second regular expressions, though parsing it as an xml object is also an option. Commented Jun 11, 2011 at 17:15
  • 5
    @reece: minimally googling the function's name would yield the php docs -- complete with examples. Commented Jun 11, 2011 at 17:15
  • 1
    @denis i understand what the functions do, but i dont undestand how i can use them together to achieve what i want to achieve. which is why i asked on here to begin with. using strpos and substr so far i have got (see edit) Commented Jun 11, 2011 at 17:24

2 Answers 2

1
$cdata_part = preg_quote('<![CDATA[images/');
$end_part = preg_quote('.jpg]]></file>');

if (preg_match("#{$cdata_part}(.+?){$end_part}#", $text, $matches)) {
    echo $matches[1];
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you have XML, then it's easy:

<?php
$xml = <<<END
<image identifier="540aa2ad-9a8d-454d-b915-605b884e76d5">
  <file><![CDATA[images/MV5BMTg5OTMxNzk4Nl5BMl5BanBnXkFtZTcwOTk1MjAwNQ@@._V1._SY317_CR0,0,214,317_.jpg]]></file>
  <title/>
  <link/>
</image>
END;

$file = (string) simplexml_load_string($xml)->file;

echo $file;

You may need to adjust the simplexml "path" a bit if you only supplied us with a partial text blob. You make it sound like there are several images within the XML, but as I don't know it's formed I cannot really tell you how to iterate over it.

Oh, and this will return the .jpg portion too, but removing an extension is as simple as substr($file, 0, -4) if you know it is .jpg.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.