Jump to content

Help taking substring in PHP

I can't figure out how to get a substring from a specific part of a string in PHP. The $output looks like this:

Array ( [result] => success [clientid] => 1 [serviceid] => [pid] => [domain] => [totalresults] => 1 [startnumber] => 0 [numreturned] => 1 [products] => Array ( [product] => Array ( [0] => Array ( [id] => 1 [clientid] => 1 [orderid] => 1 [pid] => 1 [regdate] => 2016-06-23 [name] => [translated_name] => Shared Hosting [groupname] => Shared Hosting [translated_groupname] => Shared Hosting [domain] => testdomain [dedicatedip] => [serverid] => 1 [servername] => s9 [serverip] => 68.180.229.225 [serverhostname] => fest [firstpaymentamount] => 0.00 [recurringamount] => 0.00 [paymentmethod] => mailin [paymentmethodname] => Mail In Payment [billingcycle] => Free Account [nextduedate] => 0000-00-00 [status] => Active [username] => testdoma [password] => redacted [subscriptionid] => [promoid] => 0 [overideautosuspend] => 0 [overidesuspenduntil] => 0000-00-00 [ns1] => [ns2] => [assignedips] => [notes] => [diskusage] => 0 [disklimit] => 0 [bwusage] => 0 [bwlimit] => 0 [lastupdate] => 0000-00-00 00:00:00 [customfields] => Array ( [customfield] => Array ( ) ) [configoptions] => Array ( [configoption] => Array ( ) ) ) ) ) ) 

I need to get the part of [translated_name] =>, where it says Shared Hosting. I can't seem to figure out a good way to do it.

Edited by colonel_mortis
Redacted password
Link to comment
Share on other sites

Link to post
Share on other sites

Is $output that string, or is it an array which gives that when passed through var_dump? If it's the former, use a regular expression - it would look something like this:

preg_match( '/\[translated_name\] => ([^\[]*) \[/', $output, $matches );
// The value you want is in $matches[1]

The regular expression I've used is 'find the string "[translated_name] => ", then any character other than [, as many times as possible, storing those characters in the first match of $matches, then the string " [".

 

If it's the latter (an array), you should use

$output['products']['product'][0]

 

PS. You have a password field in your output. I guess it's probably a test output, but I've replaced the contents of that field for you, just in case it is real data and you left it in by accident.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, colonel_mortis said:

Is $output that string, or is it an array which gives that when passed through var_dump? If it's the former, use a regular expression - it would look something like this:


preg_match( '/\[translated_name\] => ([^\[]*) \[/', $output, $matches );
// The value you want is in $matches[1]

The regular expression I've used is 'find the string "[translated_name] => ", then any character other than [, as many times as possible, storing those characters in the first match of $matches, then the string " [".

 

If it's the latter (an array), you should use


$output['products']['product'][0]

 

PS. You have a password field in your output. I guess it's probably a test output, but I've replaced the contents of that field for you, just in case it is real data and you left it in by accident.

 

 

I didn't even notcie the password. Spent too long trying to format the array -_- 

 

Also, 

<?php 
$substr = substr($output['products']['product'][0], $howManyCharactersInYouWantTheStringToStart, $howLongTheStringIs);
?>

 

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

59 minutes ago, colonel_mortis said:

Is $output that string, or is it an array which gives that when passed through var_dump? If it's the former, use a regular expression - it would look something like this:


preg_match( '/\[translated_name\] => ([^\[]*) \[/', $output, $matches );
// The value you want is in $matches[1]

The regular expression I've used is 'find the string "[translated_name] => ", then any character other than [, as many times as possible, storing those characters in the first match of $matches, then the string " [".

 

If it's the latter (an array), you should use


$output['products']['product'][0]

 

PS. You have a password field in your output. I guess it's probably a test output, but I've replaced the contents of that field for you, just in case it is real data and you left it in by accident.

The $output variable is technically a string I believe. Although, it is the result of 

$output = print_r($anarray, true);

So if it would be easier to do it with an array (although the array contains many other arrays), I could use the array variable. And the password is just a randomly generated string and all the data is test data as well with no real value.

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, Lunar Evolution said:

The $output variable is technically a string I believe. Although, it is the result of 


$output = print_r($anarray, true);

So if it would be easier to do it with an array (although the array contains many other arrays), I could use the array variable. And the password is just a randomly generated string and all the data is test data as well with no real value.

Yeah, there is no point having it as an array, then converting it to a string and processing it as a string rather than processing it as an array in the first place, because an array is much more efficient for accessing parts of it like that (otherwise they might as well just store arrays as a string in the first place). Use the array method.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

1 minute ago, colonel_mortis said:

Yeah, there is no point having it as an array, then converting it to a string and processing it as a string rather than processing it as an array in the first place, because an array is much more efficient for accessing parts of it like that (otherwise they might as well just store arrays as a string in the first place). Use the array method.

Yeah that makes sense. I needed it as a string for something else I was doing so I just kept it that way for this initially. The reason I was not using arrays is because I'm super new to PHP and don't fully understand them and haven't mastered them yet.

Link to comment
Share on other sites

Link to post
Share on other sites

5 minutes ago, Lunar Evolution said:

Yeah that makes sense. I needed it as a string for something else I was doing so I just kept it that way for this initially. The reason I was not using arrays is because I'm super new to PHP and don't fully understand them and haven't mastered them yet.

In general, strings shouldn't be used where other types make sense, such as here where you should use the array instead, or somewhere where you use a string rather than a number. Obviously there are some valid use cases, but they are usually pretty obvious, and often involve sending data between systems rather than just processing it within your application.

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

23 hours ago, colonel_mortis said:

In general, strings shouldn't be used where other types make sense, such as here where you should use the array instead, or somewhere where you use a string rather than a number. Obviously there are some valid use cases, but they are usually pretty obvious, and often involve sending data between systems rather than just processing it within your application.

One other quick question, in your first post you suggested some code to use, does that code work even if the [translated name] key appears multiple times in the $output variable? Or does it just recognize the first time it appears

Link to comment
Share on other sites

Link to post
Share on other sites

24 minutes ago, Lunar Evolution said:

One other quick question, in your first post you suggested some code to use, does that code work even if the [translated name] key appears multiple times in the $output variable? Or does it just recognize the first time it appears

I presume you mean something like

$array = array(
	'products' => array(
		'product' => array(
			0 => array(
				'translated_name' => 'Shared Hosting'
			),
			1 => array(
				'translated_name' => 'Private Hosting'
			)
		)
	)
);

If so, you can access the different translated_names by replacing the [0] in the array accessor with [1], etc. Alternatively, to iterate over all of them, use

foreach ($array['products']['product'] as $product) {
	// use $product['translated_name'];
}

 

HTTP/2 203

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×