Jump to content

get text before symbol


robkar32

Recommended Posts

<?php

function func($text) {
	
	$array = explode(' ', $text);
	
	for($i = 0; $i < count($array, COUNT_RECURSIVE); $i++){
		$output = substr($array[$i], strpos($array[$i], "=") + 1);
		echo $output, '<br>';
	}
}
func("arg1=Test1 arg2=test1 arg3=test3");


?>

So this code obviously outputs test1, test2 and test3, which i want it to.

 

Now how do i get the arg's as well? 

 

This is what i want to do:

if(exists('arg1' || 'arg2' || 'arg3') {
	echo "args exists";
} else {
	echo "args don't exist";
}

Hope you understand, thanks!  :happy-04:

Edited by robkar32
Link to comment
Share on other sites

Now how do i get the arg's as well? 

 

You could use explode() again.

<?php
 
function func($text) {
    $array = explode(' ', $text);
    foreach($array as $currArg){
        $currArg = explode('=', $currArg);
        print '<pre>' . print_r($currArg, true) . '</pre>';
    }
}
func("arg1=Test1 arg2=test1 arg3=test3");
 
?>
Link to comment
Share on other sites

Despite the previous concerns about why you are using this strange data format, if you simply stop using the substr function on the input you will get the arg values as well. Of course that is too easy so you muat want something else, but you didn't make that clear in your question.

Link to comment
Share on other sites

Despite the previous concerns about why you are using this strange data format, if you simply stop using the substr function on the input you will get the arg values as well. Of course that is too easy so you muat want something else, but you didn't make that clear in your question.

Yes. I just want to split up the arg values, and the values assigned to them, or the "test" values in different arrays and know where they are. Only thought of a way to get the values, and not the args. Yeah, i'm pretty new to this, and by the way i am not using this code for something big that i even need to worry about the data format.

Edited by robkar32
Link to comment
Share on other sites

With a proper data format, this would be a 30-seconds job and not take 6 hours (and counting). So maybe you should care about your decisions.

 

If you're interested in learning PHP, I'm sure we can figure out a more reasonable approach after you've described the actual problem (where does the data come from etc.).

Link to comment
Share on other sites

Yes. I just want to split up the arg values, and the values assigned to them, or the "test" values in different arrays and know where they are. Only thought of a way to get the values, and not the args. Yeah, i'm pretty new to this, and by the way i am not using this code for something big that i even need to worry about the data format.

 

If you haven't already, you could try Reply #3:happy-04:

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.