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
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
Link to comment
Share on other sites

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
Share on other sites

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]
Link to comment
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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