Jump to content

[SOLVED] string "if" functions


point86

Recommended Posts

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

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
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.
Do you mean
[code]
<?php
function 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]
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.

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.