how to extract first image of some strings - preg-match

i have this variable $img = $_POST['cur_image'];
where the content of cur_image and so $img is:
<img style="display: inline;" src="http://news.bbcimg.co.uk/media/images/65348000/jpg/_65348094_belreuters.jpg" id="1" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65356000/jpg/_65356067_65356066.jpg" id="2" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65367000/jpg/_65367308_coffee.jpg" id="3" width="100"><img style="display: none;" src="http://news.bbcimg.co.uk/media/images/65376000/jpg/_65376729_heart.jpg" id="4" width="100">
So i need to extract only link of the first image that i got i.e.
(http://news.bbcimg.co.uk/media/images/65348000/jpg/_65348094_belreuters.jpg)
and than send to db such as
mysql_query("INSERT INTO `table` (img) VALUES ('$img')");

How about something like this? (untested)
$tmp_img = $_POST['cur_image'];
$img_path_offset = strpos($tmp_img, "src=") + 4;
$img_path_end = strpos($tmp_img, "\"", $img_path_offset);
$img = substr($tmp_img, $img_path_offset, $img_path_length);
And then run your MySql..
Cheers, Viggo

Using preg_match you could do something like:
$tmp_img = $_POST['cur_image'];
$match = "src=[\"'](.+?)[\"']"
preg_match($match, $tmp_img, $matches_array);
$img = substr($matches_array[0], 4, -1);
And then your MySql..

Related

Can't fill_in value because Capybara don't see popup

As I know, when popup/modal opens it is on the top of page where I opened it from, so when try to fill value in it don't see where it need to do that.
Then tried to look inside modal with within. Tried other classes, but same Capybara::ElementNotFound: Unable to find visible css "modal183"
within('modal183') do
fill_in 'ctl00_ContentPlaceHolder1_tbId', :with => '10'
end
Tried also find with xpath, but also nothing
Capybara::ElementNotFound: Unable to find visible xpath "//input[#id='ctl00_ContentPlaceHolder1_tbId']"
<div class="modal183">
<div class="popup_Titlebar" id="PopupHeader">
<div class="TitlebarLeft">
<span id="ctl00_ContentPlaceHolder1_lblTitle">text</span>
</div>
</div>
<div class="popup_Body">
<div class="popup_TextNoTop">
<span id="ctl00_ContentPlaceHolder1_lbl1" class="label-left">text</span>
<input name="ctl00$ContentPlaceHolder1$tbId" type="text" id="ctl00_ContentPlaceHolder1_tbId" class="txtInputDec width100" onFocus="ClearTheTextbox(this, '')" onBlur="FillTheTextbox(this, '')" onkeypress="return onlyNumbers()" />
<span id="ctl00_ContentPlaceHolder1_cmrvIB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
<span id="ctl00_ContentPlaceHolder1_revIB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text
</span></a></span>
<span id="ctl00_ContentPlaceHolder1_rvTb" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
<br />
<span id="ctl00_ContentPlaceHolder1_lbl2" class="label-left">text</span>
<input name="ctl00$ContentPlaceHolder1$tb1Id" type="text" id="ctl00_ContentPlaceHolder1_tb1Id" class="txtInputDec width100 top5" onFocus="ClearTheTextbox(this, '')" onBlur="FillTheTextbox(this, '')" onkeypress="return onlyNumbers()" />
<span id="ctl00_ContentPlaceHolder1_rev1IB" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text
</span></a></span>
<span id="ctl00_ContentPlaceHolder1_rvTb1" style="color:Red;display:none;"><a href='#' class='tooltipCons'><img src='/App_Themes/Default/img/exclamation.png' alt='' /><span>text</span></a></span>
</div>
</div>
<div class="popup_Buttons">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td align="left">
<a onclick="if (!window.event) {this.disabled=true; this.style.color='grey'; var but = document.getElementById('btnCancel'); but.disabled=true; but.style.color='grey';};" id="ctl00_ContentPlaceHolder1_btnOkay" class="cool-button width80 blue" href="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$btnOkay", "", true, "cons", "", false, true))">text</a>
</td>
<td align="right">
<a id="ctl00_ContentPlaceHolder1_btnCancel" class="cool-button width80 black" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$btnCancel','')">Atcelt</a>
</td>
</tr>
</table>
</div>
</div>
Updated: Attached part where it looks like be this popup which appear after I press on button. Inspect code shows on this code. I didn't wrote this code, but trying to build automated tests on them :)
<div id="popupparent" style="display: block; height: 1e+06px; left: 0px; top: 0px;">
<script language="javascript" type="text/javascript">
function resizeIframe(obj) {
var h = "innerHeight" in window ? window.innerHeight : document.documentElement.offsetHeight;
var w = "innerWidth" in window ? window.innerWidth : document.documentElement.offsetWidth;
var doc;
if (obj.contentDocument) {
doc = obj.contentDocument;
} else if (obj.contentWindow) {
doc = obj.contentWindow.document;
} else {
return;
}
var childHeight = doc.body.scrollHeight;
var childWidth = doc.body.scrollWidth;
obj.style.height = childHeight + 'px';
var ver = getInternetExplorerVersion();
var parent = document.getElementById('termsofuse');
if (ver > -1) {
parent.style.marginTop = ((h - childHeight) / 2) - 64 + 'px';
} else {
parent.style.top = ((h - childHeight) / 2) - 64 + 'px';
}
parent.style.left = ((w - childWidth) / 2) + 'px';
parent.style.display = "block";
parent.style.visibility = "visible";
}
<div id="termsofuse" style="left: 1081px; top: 246.5px; display: block; visibility: visible;">
<iframe id="frameeditexpanse" frameborder="0" src="PopDailyCons.aspx?e=c0lEPUMwMDEwMDA3MDQmYUlEPUEwMDQ3ODIxNzAmZGF5PTImY3VyRGF0ZT0yMDE4LjA0JmN1clVzZXI9MTYwJnNvdXJjZT1hdGs=" scrolling="no" marginheight="0" marginwidth="0" class="termsOfUseFrame" style="height: 185px;"> </iframe>
<div class="popup_Buttons" style="display: none">
<input id="btnOk" value="Done" type="button">
<input id="btnCancel" value="Cancel" type="button" onclick="hideusertermwindow()">
Your within call doesn't work because you need to pass it a CSS selector. You're passing modal183 which would look for a <modal183> element, when what you really want is .modal183 to look for an element with the class modal183.
From your description it's tough to tell exactly what you're doing but it sounds like maybe you're talking about a popup window (new browser window) opened on top of the current tab. If that is the case then you'd need to swap windows in order to interact with it, along the lines of
new_win = window_opened_by do
# whatever action in the original window causes the popup window to open
click_button 'blah'
end
within_window(new_win) do
fill_in 'ctl00_ContentPlaceHolder1_tbId', :with => '10'
end

sorting obects with velocity / liferay

i have som problems with sorting a list of objects for displaying
i have this script
#set($dlService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryLocalService"))
#set($dlTypeService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileEntryTypeLocalService"))
#set($dlVersionService = $serviceLocator.findService("com.liferay.portlet.documentlibrary.service.DLFileVersionLocalService"))
#if (!$entries.isEmpty())
<div class="row-fluid site">
<div class="">
<div class="grid" id="bubble-container">
<div style="width: 50%;" class="grid-sizer"></div>
#foreach ($entry in $entries)
#**
* GET DATA
*#
#set($fileEntry = $dlService.getDLFileEntryByUuidAndCompanyId($entry.getUuid(), $entry.getCompanyId()))
#set($fileEntryTypeId = $fileEntry.getFileEntryTypeId())
#set($fileEntryType = $dlTypeService.getDLFileEntryType($fileEntryTypeId))
#set($dlFileVersion = $dlVersionService.getLatestFileVersion($fileEntry.getUserId(), $fileEntry.getFileEntryId()))
#set($fieldsMap = $fileEntry.getFieldsMap($dlFileVersion.getFileVersionId()))
#set($isDark=false)
#set($Ueberschrift = "")
#set($URL = "")
#set($Teaser = "")
#foreach ($key in $fieldsMap.keySet())
#set($URL = $fieldsMap.get($key).get("url").getValue())
#set($Ueberschrift = $fieldsMap.get($key).get("ueberschrift").getValue())
#set($Teaser = $fieldsMap.get($key).get("teaser").getValue())
#set($isDark = $fieldsMap.get($key).get("schwarzerHintergrund").getValue())
#end
<div style="width: 50%; position: absolute; left: 0%; top: 0px;" class="grid-item">
#set($imageURL = $dlUtil.getPreviewURL($entry, $entry.getFileVersion(), $themeDisplay, ""))
#set ($additionalCss = "")
#if ($isDark)
#set ($additionalCss = "black")
#end
<a href="$URL">
<div class="bubble $additionalCss">
<h2>$Ueberschrift</h2>
<p>$Teaser</p>
</div>
<div class="bubble-icon">
<img src="$imageURL"/>
</div>
</a>
</div>
#end
</div>
</div>
</div>
#end
now i want to sort the objects in the outer foreach loop
i've read something about it at https://velocity.apache.org/tools/devel/apidocs/org/apache/velocity/tools/generic/SortTool.html but i'm not ne able to expand the script and sort the objects
it must be something like
#foreach ($entry in $entries_sort.Sort($entries,"getUuid"))
does anyone have a hint for me?
best regards
In Velocity you have available $sortTool which provides sort method. Example of usage:
#foreach ($curEntry in $sortTool.sort($entries, 'entryId:asc'))
$curEntry
#end

create empty 1px image

I create image tag in HTML
var img = document.getElementById('image');
alert(img.width)
<div width="500">
<img id="image" width="100%" height="1"></img>
</div>
In all browsers img.width will be 500px. But in Firefox it will be 0. But if set the src in image tag it work.
Is it possible to set 1px transparent image without using any files(png, jpg, ...)
var img = document.getElementById('image');
alert(img.width)
Thanx to LinuxDisciple I found a solution to the problem
<div width="500">
<img src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7" id="image" width="100%" height="1"></img>
</div>

Dompdf make a blank page inside the generated pdf Document

i have tried creating pdf files with dynamic content (generated with db data)
DomPdf was perfectly working with it.
but when i add some addtional section it breaks the content in the middle of div blocks.
So i added page-break-inside: avoid; then that problem solved.
now it makes a blank page after that div element in the pdf document !
Any Solutions? Thanks in advance !
Here is the Code within the php :
if(mysql_num_rows($get_options_query) > 0){// we have options available
$color2 = 'f9ebd4';
$quote_items .= '<div style="page-break-inside: avoid; width: 760px; height: auto;"><div style="width: 760px; height: auto; background-color: #f19221;padding:5px; ">Options:</div>';
while($get_options = mysql_fetch_array($get_options_query)){
$prod_opt_col_images='';
$all_prod_opt_col=$db->get_results("SELECT colour_name, colour_image
FROM prod_variations
JOIN prod_colours ON prod_variations.product_colour = prod_colours.colour_name
WHERE prod_variations.product_id ='".$item['product_id']."'");
if(count($all_prod_opt_col)>0){
$prod_opt_col_images='<br />'.'Available in:<br />';
foreach($all_prod_opt_col as $row_prod_opt_col ){
$prod_opt_col_images.='<img src="'.URL.'/images/colours/mat/'.$row_prod_opt_col->colour_image.'" width="25" height="25" alt="'.$row_prod_opt_col->colour_name.'" "/> ';
}
}
if($prod_opt_col_images!=''){
$prod_opt_col_images.='<br />';
}
$quote_items .= '<div class="options" style="background-color: #'.$color2.';">
<div class="divCell1"> '.$get_options['qp_prod_qty'].'</div>
<div class="divCell2"> '.str_replace("<br>","<br />",$get_options['qp_prod_name']).'<br />'.str_replace("<br>","<br />",$get_options['qp_prod_desc']).$prod_opt_col_images.'</div>
<div class="divCell3"> <img src="'.URL.'/images/products/'.$get_options['qp_prod_image'].'" width="75" height="75" alt="Quote Product" /></div>
<div class="divCell4"> $'.number_format($get_options['qp_prod_price'],2,'.',',').'</div>
<div class="divCell5"> </div>
</div></div>';
}
}

XPath - Get textcontent() and HTML

Lets say I have the following HTML:
<div class="some-class">
<p> some paragraph</p>
<h2>a heading</h2>
</div>
I want to grab everything in <div class='some-class'>, including the HTML. The following only grabs the text:
$descriptions = $xpath->query("//div[contains(#class, 'some-class')]");
foreach($descriptions as $description)
print $description->textContent;
Whats the best way of getting the contained HTML tags as well?
Use this function - I've never found any built in function but this works well:
function getInnerHTML($node)
{
$innerHTML = "";
$children = $node->childNodes;
foreach ($children as $child) {
$tmp_doc = new DOMDocument();
$tmp_doc->appendChild($tmp_doc->importNode($child,true));
$innerHTML .= $tmp_doc->saveHTML();
}
return $innerHTML;
}
I believe you are looking to retrieve the outerXml - have a look at DOMDocument::saveXML. Or have I misunderstood you - do you just need the xml serialization of the <div> element and its attribute axis?
Edit I mean do you want:
<div class="some-class">
<p> some paragraph</p>
<h2>a heading</h2>
</div>
or just
<div class="some-class" />
?

Resources