PHP - How to extract a number from the string? - php

I have a string in the pattern similar to:
john.smith 9.5 9.49296 Active john.s#site.com +123456789
and I just want to echo "9.5" out of it which is next to "smith" using PHP.
Update:
sorry guys...just noticed that it is an xml file, couldn't see properly in safari, checked in firefox and it is displayed as:
<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s#site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>
Now what would be the php code to echo "9.5"
Thanks for your earlier answers...

Try splitting on whitespace (seem to be delimited by that)
$parts = preg_split('/\s+/', $input);
print $parts[1];

a quick and durty way
<?php
$string="john.smith 9.5 9.49296 Active john.s#site.com +123456789";
$array = explode(" ",$string);
echo $array[1];
?>

$string="john.smith 9.5 9.49296 Active john.s#site.com +123456789";
$array=explode(" ",$string);
Then your number would be:
echo $array[1];

This will work
<?php
$string = "<GetUserInfo>
<Customer>john.smith</Customer>
<Balance>9.5</Balance>
<SpecificBalance>9.49296</SpecificBalance>
<Status>False</Status>
<EmailAddress>john.s#site.com</EmailAddress>
<Phone>+1234567890</Phone>
</GetUserInfo>";
$xml = simplexml_load_string($string);
echo $xml->Balance;
?>

Related

I want to echo php text but not between ()

I want to echo php text but not between (). Some thing like this =
<?php
$text = "Barry(male)";
echo $text;
?>
output =
Barry
How can i do this?
You can use preg_replace to substitute whatever is between parenthes (and the parentheses themselves) with an empty string. Like this:
<?php
$text = "Barry(male)";
echo preg_replace('#\(.*\)#', '', $text);
?>
Please note: since you didn't specify your string format, I'm assuming that the parenthesized text appears just once in the string and that there aren't nested parenthes. Otherwise, this doesn't work as expected.
Something like:
$text = "Barry(male)";
$split = explode("(", $text);
echo $split[0];
// "Barry"

separate each words in php

I had an value in database like "demo text" . I want to display this content from the db in the view page as
Html code that i am using is like this <h2>Demo<span>Text</span></h2> , is there any solution for seperate each words and use one for h2 and other for span. I am using php codeigniter for the project , I don't know that whether the way i explained my problem is correct or not .
Yes you can so it with explode
if you have stored at least 2 words with space. try following
$demo ="demo text";
$arr = explode(" ",$demo);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
EDIT
If you have more words and want to split first word only you can pass limit parameter in explode
$demo ="Pligrimage to Marian Shrines";
$arr = explode(" ",$demo,2);
$str = "<h2>".$arr[0]."<span>".$arr[1]."</span></h2>";
echo $str;
DEMO
I think you are looking for something like this!
$your_string = "Hello Houston! We have a problem!";
$my_array = explode(" ",trim($your_string));
$output = "<h2>";
foreach($my_array as $a_word){
if ($a_word === reset($my_array))
$output .= $a_word;
else
$output .= " <span>". $a_word . "</span>";
}
$output .= "</h2>";
print $output;

I need to split text delimited by paragraph tag

$text = "<p>this is the first paragraph</p><p>this is the first paragraph</p>";
I need to split the above into an array delimited by the paragraph tags. That is, I need to split the above into an array with two elements:
array ([0] = "this is the first paragraph", [1] = "this is the first paragraph")
Remove the closing </p> tags as we don't need them and then explode the string into an array on opening </p> tags.
$text = "<p>this is the first paragraph</p><p>this is the first paragraph</p>";
$text = str_replace('</p>', '', $text);
$array = explode('<p>', $text);
To see the code run please see the following codepad entry. As you can see this code will leave you with an empty array entry at index 0. If this is a problem then it can easily be removed by calling array_shift($array) before using the array.
For anyone else who finds this, don't forget that a P tag may have styles, id's or any other possible attributes so you should probably look at something like this:
$ps = preg_split('#<p([^>])*>#',$input);
This is an old question but I was not able to find any reasonable solution in an hour of looking for stactverflow answers. If you have string full of html tags (p tags) and if you want to get paragraphs (or first paragraph) use DOMDocument.
$long_description is a string that has <p> tags in it.
$long_descriptionDOM = new DOMDocument();
// This is how you use it with UTF-8
$long_descriptionDOM->loadHTML((mb_convert_encoding($long_description, 'HTML-ENTITIES', 'UTF-8')));
$paragraphs = $long_descriptionDOM->getElementsByTagName('p');
$first_paragraph = $paragraphs->item(0)->textContent();
I guess that this is the right solution. No need for regex.
edit: YOU SHOULD NOT USE REGEX TO PARSE HTML.
$text = "<p>this is the first paragraph</p><p>this is the first paragraph</p>";
$exptext = explode("<p>", $text);
echo $exptext[0];
echo "<br>";
echo $exptext[1];
//////////////// OUTPUT /////////////////
this is the first paragraph
this is the first paragraph
Try this code:
<?php
$textArray = explode("<p>" $text);
for ($i = 0; $i < sizeof($textArray); $i++) {
$textArray[$i] = strip_tags($textArray[$i]);
}
If your input is somewhat consistent you can use a simple split method as:
$paragraphs = preg_split('~(</?p>\s*)+~', $text, PREG_SPLIT_NO_EMPTY);
Where the preg_split will look for combinations of <p> and </p> plus possible whitespace and separate the string there.
As unnecessary alternative you can also use querypath or phpquery to extract only complete paragraph contents using:
foreach (htmlqp($text)->find("p") as $p) { print $p->text(); }
Try the following:
<?php
$text = "<p>this is the first paragraph</p><p>this is the first paragraph</p>";
$array;
preg_replace_callback("`<p>(.+)</p>`isU", function ($matches) {
global $array;
$array[] = $matches[1];
}, $text);
var_dump($array);
?>
This can be modified, putting the array in a class that manage it with an add value method, and a getter.
Try this.
<?php
$text = "<p>this is the first paragraph</p><p>this is the first paragraph</p>";
$array = json_decode(json_encode((array) simplexml_load_string('<data>'.$text.'</data>')),1);
print_r($array['p']);
?>

Replace c:\ with file:\\\c| using PHP

I have the local file path as c:\new folder\pdf\today\k.pdf I want to replace the c:\ with file:\\c|
I tried str_replace('','',) but I get error due to the slash, no stripslash will not work.
Thanks
Jean
Isn't c:\new folder\pdf\today\k.pdf to file:///c|new folder/pdf/today/k.pdf?
If so, following will work, without regex
$x='c:\new folder\pdf\today\k.pdf';
$x='file:///'.str_replace('\\','/',str_replace(':\\','|',$x));
file:///c|new folder/pdf/today/k.pdf will return
Try:
$string = 'c:\new folder\pdf\hello.pdf';
$new_str = str_replace("c:\\", "file:\\\\\\c|", $string);
echo $new_str;
Result:
file:\\\c|new folder\pdf\hello.pdf
You can do this:
<?php
$a = 'c:\new folder\pdf\today\k.pdf';
$a = str_replace('c:\\','file:\\\\\\c|',$a);
var_dump($a); // print string(36) "file:\\\c|new folder\pdf\today\k.pdf"
?>

How to remove a text from a variable? (php)

I have a variable $link_item, it's used with echo and gives the strings like
<span class="name">Google</span>http://google.com
How to remove "<span class="name">Google</span>" from string?
It should give just "http://google.com".
Heard it can be done with regex(), please help.
Without regex:
echo substr($link_item, stripos($link_item, 'http:'))
But this only works if the first part (i.e. <span class="name">Google</span>) never contains http:. If you can assure this: here you go :)
Reference: substr, stripos
Update:
As #Gordon points out in his comment, my code is doing the same as strstr() already does. I just put it here in case one does not read the comments:
echo strstr($link_item, 'http://');
$string = '<span class="name">Google</span>http://google.com';
$pieces = explode("</span>",$string);
//In case there is more than one span before the URL
echo $pieces[count($pieces) -1];
Solved:
$contents = '<span class="name">Google</span>http://google.com';
$new_text = preg_replace('/<span[^>]*>([\s\S]*?)<\/span[^>]*>/', '', $contents);
echo $new_text;
// outputs -> http://google.com
Don't use a regex. Use a HTML parser to extract only the text you want from it.
Made myself
$link_item_url = preg_replace('#<span[^>]*?>.*?</span>#si', '', $link_item);
This will remove any <span + something + </span> from variable $link_item.
Thanks for all.

Categories