I have this html
<span class="item-title">Title</span>
<span class="item-cat">sub-text</span>
I have in database string like Title sub-text. How can I place sub-text in bottom <span> when str_replace match space? I've tried something like this
<span class="item-title">'.str_replace(' ',"<span class="item-cat">sub-text</span>",$row['category']).'</span>
But seems not right because I don't know how to divide the string and show only second part in second div. Any help is appreciated.
In case of sub-text won't have any spaces you can simply use explode() function create two strings. One for title and another one for sub-text.
Code would look seomthing like this,
<?php
$mainTitle="Title sub-text";
$parts=explode(" ",$mainTitle);
?>
<span class="item-title"><?php echo $parts[0]; ?></span>
<span class="item-cat"><?php echo $parts[1]; ?></span>
<?php // Other code ?>
explode() just split the string by space( in this case) which will create array of two elements, of which first one is title and second
one would be sub-text.
To split this you can use this
<?php
$str = 'Title sub-text';
$myarray = str_word_count ($str,1);
// echo $myarray[0]; // Title
//echo $myarray[1]; // sub-text
?>
<span class="item-title"><?php echo $myarray[0]; ?></span>
<span class="item-cat"><?php echo $myarray[1]; ?></span>
For more info about str_word_count please read http://php.net/manual/en/function.str-word-count.php
Note: You can also use explode.
Related
[PHP]I have a variable for storing strings (a BIIGGG page source code as string), I want to echo only interesting strings (that I need to extract to use in a project, dozens of them), and they are inside the quotation marks of the tag
but I just want to capture the values that start with the letter: N (news)
[<a href="/news7044449/exclusive_news_sunday_"]
<a href="/n[ews7044449/exclusive_news_sunday_]"
that is, I think you will have to work with match using: [a href="/n]
how to do that to define that the echo will delete all the texts of the variable, showing only:
note that there are other hrefs tags with values that start with other letters, such as the letter 'P' : href="/profiles... (This does not interest me.)
$string = '</div><span class="news-hd-mark">HD</span></div><p>exclusive_news_sunday_</p><p class="metadata"><span class="bg">Czech AV<span class="mobile-hide"> - 5.4M Views</span>
- <span class="duration">7 min</span></span></p></div><script>xv.thumbs.preparenews(7044449);</script>
<div id="news_31720715" class="thumb-block "><div class="thumb-inside"><div class="thumb"><a href="/news31720715/my_sister_running_every_single_morning"><img src="https://static-hw.xnewss.com/img/lightbox/lightbox-blank.gif"';
I imagine something like this:
$removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n = ('/something regex expresion I think /' or preg_match, substring?);
echo $string = str_replace($removes_everything_except_values_from_the_href_tag_starting_with_the_letter_n,'',$string);
expected output: /news7044449/exclusive_news_sunday_
NOTE: it is not essential to be through a variable, it can be from a .txt file the place where the extracts will be extracted, and not necessarily a variable.
thanks.
I believe this will help her.
<?php
$source = file_get_contents("code.html");
preg_match_all("/<a href=\"(\/n(?:.+?))\"[^>]*>/", $source, $results);
var_export( end($results) );
Step by Step Regex:
Regex Demo
Regex Debugger
To get just the links out of the $results array from Valdeir's answer:
foreach ($results as $r) {
echo $r;
// alt: to display them with an HTML break tag after each one
echo $r."<br>\n";
}
My thing is when I echo my php code it doesnt echo the code it echo's numbers in return I am trying to add a PHP code to my website so every week the text on the website would update from a stored file like quotes.txt. Is there something wrong with my php any suggestions? Here is a screenshot of what happens https://gyazo.com/fba5fc414228b1ab2a79bb877642477a
My Code :
<div class="wrapper">
<div class="teachings">
<h1 id="teach">Teaching's</h1>
<hr>
<?php
$text = file_get_contents("quotes.txt");
$text = trim($text); //This removes blank lines so that your
//explode doesn't get any empty values at the start or the end.
$array = explode(PHP_EOL, $text);
$lineNumber = date('s');
echo "<p>$lineNumber</p>";
?>
<h1>Weekly Teachings :</h1>
<br>
</div>
</div>
You are echoing the currect second value from a date('s') function call
I assume you want to echo the number of lines in the array
$array = explode(PHP_EOL, $text);
$lineNumber = count($array);
echo "<p>$lineNumber</p>";
But if you intended something else, add a comment I I will attempt to change this answer accordingly
RE: Your comment below, then you want to do
echo "<p>{$array[0]}</p>";
What you want to do is
echo $array[$lineNumber];
That will echo the quote whose line number is the week number.
I have a long text and I would like to add a no-wrap after specific key words. Lets say: 'Mr.', 'the', 'an' the only problem is I do not know what word will be after the key.
So if I have a text like:
... there is an elephant in the room ...
script should change it to:
... there is <span class="no-wrap">an elephant</span> in <span class="no-wrap"> the room</span> ...
I know that it should be done with regular expression of some sort but I am really bad at those. So any tips on how to do this in php?
Capture Mr., the, an strings and also the following word into a group.
(\b(?:Mr\.|the|an)\h+\S+)
Replacement string:
<span class="no-wrap">$1</span>
DEMO
Code:
<?php
$string = "... there is an elephant in the room ...";
echo preg_replace('~(\b(?:Mr\.|the|an)\h+\S+)~', '<span class="no-wrap">$1</span>', $string)
?>
Output:
... there is <span class="no-wrap">an elephant</span> in <span class="no-wrap">the room</span> ...
How I can replace string inside some text to getting this string without this "pattern"?
For example I trying replace %%some text%% to
<span class="spoiler">some text</span>
preg_replace("'%%[\w\s]+%%'siu",'<span class="spoiler">$0</span>',$description);
This will do what you are looking for:
$description = '%%some text%%';
$fixed_description = preg_replace("~%%([\w\s]+?)%%~siu",'<span class="spoiler">$1</span>',$description);
echo $fixed_description;
Output:
<span class="spoiler">some text</span>
My html code is as follows
<span class="phone">
i want this text
<span class="ignore-this-one">01234567890</span>
<span class="ignore-this-two" >01234567890</span>
<a class="also-ignore-me">some text</a>
</span>
What I want to do is extract the 'i want this text' leaving all of the other elements behind. I've tried several iterations of the following, but none return the text I need:
$name = trim($page->find('span[class!=ignore^] a[class!=also^] span[class=phone]',0)->innertext);
Some guidance would be appreciated as the simple_html_dom section on filters is quite bare.
what about using php preg_match (http://php.net/manual/en/function.preg-match.php)
try the below:
<?php
$html = <<<EOF
<span class="phone">
i want this text
<span class="ignore-this-one">01234567890</span>
<span class="ignore-this-two" >01234567890</span>
<a class="also-ignore-me">some text</a>
</span>;
EOF;
$result = preg_match('#class="phone".*\n(.*)#', $html, $matches);
echo $matches[1];
?>
regex explained:
find text class="phone" then proceed until the end of the line, matching any character using *.. Then switch to a new line with \n and grab everything on that line by enclosing *. into brackets.
The returned result is stored in the array $matches. $matches[0] holds the value that is returned from the whole regex, while $matches[1] holds the value that is return by the closing brackets.