point86 Posted January 1, 2007 Share Posted January 1, 2007 Hi,I have a large string which is made up of three concatanated smaller strings seperated by a full stop "." and a "*".$query = $_GET['string'];$query2 = $query.'.'.$_GET['string2'];$query3 = $query2.'*'.$_GET['string3'];$terms = split(" ", $query3);Thus, I have the string terms = "string.string2*string3."Now how do I write an "if" function that says;Read through the string and do X. When/if you get to a full stop, do Y. When /if you get to a *, do Z.Thanks,Point. Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/ Share on other sites More sharing options...
kenrbnsn Posted January 1, 2007 Share Posted January 1, 2007 Do you want to do "Y" if there is a "." in the string and "Z" if the is a "*" in the string? Or do you want to do "Y" for each "." and "Z" for each "*". Also, do "Y" and "Z" care about the substrings in the main string?The answers to these questions determine how the solution to your question is written.Ken Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-150781 Share on other sites More sharing options...
point86 Posted January 1, 2007 Author Share Posted January 1, 2007 Hi,Sorry I should have been more specific.What I mean to say is that I want the code to do the following:The string is made up of words which are then highlighted in different colours (I've got the colour bit working). So, with the following string:terms = "string.string2*string3"It could be (for example)terms ="hello.there*great"I want the code to say to the system:For the first substring ("hello"), do X for that bit. When you get to the ".", this is the second bit, ("there"), so do Y for that bit, until you get to the "*" ("great"), this is the third part, so do Z for that bit.Hope that cleared things up!Many thanks,Point. Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-150910 Share on other sites More sharing options...
fert Posted January 1, 2007 Share Posted January 1, 2007 [code]$temp=explode(".",$string);$first_word=$temp[0];$temp=explode("*",$temp[0]);$second_word=$temp[0];$third_word=$temp[1];[/code] Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-150912 Share on other sites More sharing options...
corbin Posted January 1, 2007 Share Posted January 1, 2007 What if a user actually puts in a . though? Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-150914 Share on other sites More sharing options...
Barand Posted January 1, 2007 Share Posted January 1, 2007 Do you mean[code]<?phpfunction X ($val) { echo "<p>X: $val</p>";}function Y ($val) { echo "<p>Y: $val</p>";}function Z ($val) { echo "<p>Z: $val</p>";}$str = "Hello.World*great";list ($x, $tmp) = explode ('.', $str);list ($y, $z) = explode ('*', $tmp);X($x);Y($y);Z($z);?>[/code] Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-150915 Share on other sites More sharing options...
point86 Posted January 2, 2007 Author Share Posted January 2, 2007 The user won't ever enter a "." or a "*".I have the string bit sorted. I basically need a glorified "if" function in PHP that takes the string, reads through it and says;"Read through the string and do something (X). when you come to ".", do something different (Y) and when you come to "*", do something different (Z).ie.Do (X) if (you see a ".") { do Y } if (you see a "*") { do Z }Thanks,Point. Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-151358 Share on other sites More sharing options...
kenrbnsn Posted January 2, 2007 Share Posted January 2, 2007 Try this:[code]<?php$str = "this is some string.with dots.and asterisk*in it*junk";for ($i=0;$i<strlen($str);$i++) { if ($str[$i] == '.') {//// do Y// } if ($str[$i] == '*') {//// do Z// }}?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-151359 Share on other sites More sharing options...
point86 Posted January 4, 2007 Author Share Posted January 4, 2007 That's great, thanks!!:D Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-152607 Share on other sites More sharing options...
corbin Posted January 4, 2007 Share Posted January 4, 2007 It's based on GET so a user could easily change the url to have '.' in it... Link to comment https://forums.phpfreaks.com/topic/32449-solved-string-if-functions/#findComment-152608 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.