papaface Posted February 24, 2007 Share Posted February 24, 2007 Hello, I was wondering if someone could tell me how I would find the value between the two :'s. e.g :iwantthisvalue: regards Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/ Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Check out substr() and strpos() Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192598 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 I dont think I can use those because :value: would be in a long peice of text and there are multiple :'s with values i want. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192601 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 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 />'; }?> Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192610 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 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. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192624 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Okay...and the code I posted works perfectly fine. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192625 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 I didnt know you replied. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192626 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 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 Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192633 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 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; ?> Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192651 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 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... Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192666 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 You used $value and $values, pick one or the other. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192671 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 I dont understand because $value['_posts_content'] is the string I am trying to analyse. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192673 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 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 Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192675 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Warning: Invalid argument supplied for foreach() in page.php on line 104 104: foreach($values AS $v){ Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192677 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 Yeah but did it print anything on the print_r($values)? What about for $copy? If not, your $value[_posts] thing isn't set. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192680 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 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. Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192682 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Oh it working now, it appears it was a problem with the value in the :'s Thanks for your help Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192686 Share on other sites More sharing options...
papaface Posted February 24, 2007 Author Share Posted February 24, 2007 Ah I have realised that it only shows the error when there are no :value: in the string. How can I get it to check if they are any to be replaced, and if there are some, to do the replacing, otherwise just skip it? Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192699 Share on other sites More sharing options...
Jessica Posted February 24, 2007 Share Posted February 24, 2007 if(count($values)){ //foreach in here } Link to comment https://forums.phpfreaks.com/topic/39864-solved-find-value-between/#findComment-192716 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.