Jump to content

[SOLVED] Find value between ::


papaface

Recommended Posts

Yes, you can...I started to write you an example but then firefox crashed. I wrote you an example. You could also try explode, and other similar functions.

<?php
$str = "This :is: a string :with: values :in it:";
while(strpos($str, ':') !== FALSE){
print 'String: '.$str.'<br />';
$str = substr($str, (strpos($str, ':')+1));
$value = substr($str, 0, strpos($str, ':'));
$str = substr($str, (strpos($str, ':')+1));
print 'Found Value: '.$value.'<br />';
}?>

Ive tried:

$string = ":bad::happy::mdr::tongue::wink::zzz::whip::^^:";
$find = explode(":", $string);

foreach ($find as $key => $value)
{
$found = str_replace(array($value),array('<img src="images/smileys/'.$value.'.gif" width="18" height="18" />'),$string);
echo $found . "<br>";	
}

But i get:

:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:<img src="images/smileys/bad.gif" width="18" height="18" />::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::<img src="images/smileys/cry.gif" width="18" height="18" />::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::<img src="images/smileys/happy.gif" width="18" height="18" />::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::<img src="images/smileys/mdr.gif" width="18" height="18" />::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::<img src="images/smileys/tongue.gif" width="18" height="18" />::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::<img src="images/smileys/wink.gif" width="18" height="18" />::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::<img src="images/smileys/zzz.gif" width="18" height="18" />::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::<img src="images/smileys/whip.gif" width="18" height="18" />::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>:bad::happy::mdr::tongue::wink::zzz::whip::<img src="images/smileys/^^.gif" width="18" height="18" />:<br>:bad::happy::mdr::tongue::wink::zzz::whip::^^:<br>

outputted.....

 

EDIT: didnt know you replied. I will see if i can work your example into this.

You code works perfectly, and it is what I need, but I am also trying to replace (AND the ::'s) each value in between :'s with a dynamic image tag.

$str = ":bad::happy::mdr::tongue::wink::zzz::whip::^^:";
while(strpos($str, ':') !== FALSE)
{
$str = substr($str, (strpos($str, ':')+1));
$value = substr($str, 0, strpos($str, ':'));
$str = substr($str, (strpos($str, ':')+1));
print 'Value: '.$value.'<br />';
//$found = str_replace(array($value),array('<img src="images/smileys/'.$value.'.gif" width="18" height="18" />'),$str);
//echo $found;
}
?>

 

But my code just doesnt work, im crap at things like this lol....

So to clarify, I should be getting:

<img src="images/smileys/bad.gif" width="18" height="18" />
<img src="images/smileys/cry.gif" width="18" height="18" />
<img src="images/smileys/happy.gif" width="18" height="18" />

etc

Try this one.

<?php
$str = ":bad::happy::mdr::tongue::wink::zzz::whip::^^:";
$copy = $str;
while(strpos($str, ':') !== FALSE){
$str = substr($str, (strpos($str, ':')+1));
$values[] = substr($str, 0, strpos($str, ':'));
$str = substr($str, (strpos($str, ':')+1));
}

foreach($values AS $v){
$copy = str_replace(":$v:", '<img src="images/smileys/'.$v.'.gif" />', $copy);
//Or you could do $copy = str_replace(":$v:", "<img src=\"images/smileys/$v.gif\" />", $copy);
}
print $copy;
?>

Thanks, it worked on your example, but when I tried to implement it into my code I got this error:

Warning: Invalid argument supplied for foreach() in page.php on line 101

 

I am using:

	$copy = $value['_posts_content'];
	while(strpos($value['_posts_content'], ':') !== FALSE){
	$value['_posts_content'] = substr($value['_posts_content'], (strpos($value['_posts_content'], ':')+1));
	$values[] = substr($value['_posts_content'], 0, strpos($value['_posts_content'], ':'));
	$value['_posts_content'] = substr($value['_posts_content'], (strpos($value['_posts_content'], ':')+1));
	}

	foreach($values AS $v){
	$copy = str_replace(":$v:", '<img src="images/smileys/'.$v.'.gif" />', $copy);
	//Or you could do $copy = str_replace(":$v:", "<img src=\"images/smileys/$v.gif\" />", $copy);
	}
	$value['_posts_content'] = $copy;

Line 101 is foreach($values AS $v){

I cant see why it isnt working...

Ah I see, I read too fast.

Do this:

<?php
$copy = $value['_posts_content'];
print $copy;
while(strpos($value['_posts_content'], ':') !== FALSE){
$value['_posts_content'] = substr($value['_posts_content'], (strpos($value['_posts_content'], ':')+1));
$values[] = substr($value['_posts_content'], 0, strpos($value['_posts_content'], ':'));
$value['_posts_content'] = substr($value['_posts_content'], (strpos($value['_posts_content'], ':')+1));
}

print_r($values);

foreach($values AS $v){
$copy = str_replace(":$v:", '<img src="images/smileys/'.$v.'.gif" />', $copy);
}
$value['_posts_content'] = $copy;
?>

 

And see what it puts out

Ive changed my source to $value['posts_content']

For this code:

$copy = $value['posts_content'];
//print $copy;
while(strpos($value['posts_content'], ':') !== FALSE){
$value['posts_content'] = substr($value['posts_content'], (strpos($value['posts_content'], ':')+1));
$values[] = substr($value['posts_content'], 0, strpos($value['posts_content'], ':'));
$value['posts_content'] = substr($value['posts_content'], (strpos($value['posts_content'], ':')+1));
}

//print_r($values);

foreach($values AS $v){
$copy = str_replace(":$v:", '<img src="images/smileys/'.$v.'.gif" />', $copy);
}
$value['posts_content'] = $copy;

I get outputted:


Warning: Invalid argument supplied for foreach() in page.php on line 104
Test User: This is a test shoutbox.

Warning: Invalid argument supplied for foreach() in page.php on line 104
Test User: You can use it as you wish.

The content without :value: is coming out fine, but it appears anything with a :value: to be processed isnt.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.