Break string into fields [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string input using PHP that look like this:
Log,,t1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23
I want to save it into MySQL
field:reading
t1:12.12
t2:20.23
t3:30.00
t4:50.20
Temp:11.23
Can anyone give me an direction?

Begin you have to parse to rows and columns
$string = 't1,12.12:t2,20.23:t3,30.00:t4,50.20:11.23';
$rows = explode(':', $string);
$table = array();
foreach(rows as $row) {
$table[] = explode(',', $row);
}

You can use regex below:
if (preg_match_all('/[a-z]+\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}
The regex can be a bit simpler if each field will called "t+number":
if (preg_match_all('/t\d+,\d+\.\d+|\d+\.\d+/i', $string, $matches) {
var_dump($matches[0]);
}

Related

i need Apostrophe in a variable result MYSQL [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I receive data in this format like:
C00001,C00302,C00303,C00287,C00286,C00285,C00017
in a variable but i need in this shape:
'C00001','C00302','C00303','C00287','C00286','C00285','C00017'
i am new in mysql kindly any help
You can explode the data, modify it, then re-implode it.
$data = 'C00001,C00302,C00303,C00287,C00286,C00285,C00017';
$arr = explode(',', $data);
$new_arr = array_map(function($a){
return "'{$a}'";
}, $arr);
$new_data = implode(',', $new_arr);

Remove specific match from a string [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to remove a particular match of characters from a string.
For example i have strings;
topics-p10-new-model-cars
topics-p20-new-model-cars
topics-p30-new-model-cars
topics-p40-new-model-cars
Then i need the results as,
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
topics-new-model-cars
That means i want to remove p10-,p20-,etc..
.Those are the page numbers. It may be any number..
How can i do this..? Thanks in advance
Try this:
$result = preg_replace('/\-p\d+/', '', $string);
Note: I'm assuming that the string format does not change (I mean this [topics-p10-new-model-cars]). If my assumption is right.
Then you can do this
if (textBox1.Text.Contains("-p10-"))
{
//topics-p10-new-model-cars
String[] splited = textBox1.Text.Split(new char[] {'-'});
String rString = String.Format("{0}-{1}-{2}-{3}",
splited[0],splited[2],splited[3],splited[4]);
MessageBox.Show(rString);
}
//OR This method
if (textBox1.Text.Contains("-p10-"))
{
String result = textBox1.Text.Replace("p10-", "");
MessageBox.Show(result);
}
With 'preg-replace'::
For, example:
<?
echo preg_replace('/p[0-9]+\-/', '', 'topics-p10-new-model-cars');
?>
Follow this link:
http://www.php.net/manual/en/function.preg-replace.php

how to find array with regex [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have an array that depending of my input, will return different name for a particular node. For example below I the name of the key node is n1:ModemProduct1207 but it could be also n1:ModemProduct1308.
I was wondering is there is a way to have something like this:
$array ['n1:ModemProducts'] ['n1:ModemProduct'. (regex here like"n1:ModemProduct[\d0-9]{4}+")];
$modemProducts = $array ['n1:ModemProducts'] ['n1:ModemProduct1207'];
I tried couple of options but could not get it to work.
Little function for one-dimensional array:
function filterProducts($array, $pattern){
$result = array();
foreach($array as $key => $value){
if (preg_match($pattern, $key)){
$result[$key] = $value;
}
}
return $result;
}
Usage:
$results = filterProducts($array['n1:ModemProducts'], '/n1:ModemProduct[\d]{4}/');
$array = array('Armenia', 'America', 'Algeria', 'India', 'Brazil', 'Croatia', 'Denmark');
$fl_array = preg_grep('/^A.*/', $array);
print_r($fl_array);
for referance you can see . http://ask.amoeba.co.in/php-search-in-an-array-for-values-matching-a-pattern-regex-wildcard/
I am Trying to solve your problam
Hope this Help
Thanks
Anand

Find a string between a character and a string? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a string : http://www.mywebsite/456754567/531613490.htm?menu=contact
I want to get the value between "/" and ".htm". Here : 531613490
Any idea ?
if ( preg_match('~[^/]+(?=\.htm)~', $string, $matches) ) {
echo $matches[0];
}
Here's a demo: http://codepad.viper-7.com/5kjEj6
I don't know how flexible you want your script to be, but here's my try:
It always takes the last part of the path
It won't fail for http://www.mywebsite/456754567.htm/531613490.htm?menu=contact' as Joseph Silber's solution does.
<?php
$path = parse_url('http://www.mywebsite/456754567/531613490.htm?menu=contact', PHP_URL_PATH);
$pathParts = explode('/', $path);
$fullFilename = array_pop($pathParts);
// better use something like lastIndexOf(), this won't fail for 'xxx.abc.htm'
$filename = substr($fullFilename, 0, strpos($fullFilename, '.'));
var_dump( $filename );

Trim string after and before a character [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to trim this string after "=" and before ";"
com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;
so that it echoes 8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75
You can use the str_replace function to replace multiple values by, in this case, nothing:
str_replace(array('com=', ';'), '', $string);
This can also work:
parse_str(trim(
'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;'
, ';'));
echo "$com\n";
Try this:
$value="aaaacom=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;";`
$pos=strpos($value,"=");
echo substr($value,$pos+1,strlen($value)-($pos+2));
You can use a regex:
$str = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
echo preg_replace('/([a-z]+)=(.*);/', '\2', $str);
But it looks like a .ini file, then maybe you want this:
Try this:
$ini = 'com=8af14f8b9d5be16e807ab9e0ed1d7edb:b6668c681f9d3bd2ad4ae31a8b6c7f2859939a75;';
$parsed_ini = parse_ini_string($ini);
echo $parsed_ini['com'];

Categories